【问题标题】:Product of an Integer Array with Limit带极限的整数数组的乘积
【发布时间】:2016-11-15 12:27:35
【问题描述】:

如何在 Java 中获得具有特定限制的整数数组的乘积?

让我们说:

  • 数组未排序
  • 数组可以包含从 -99 到 99 的任意数字。
  • 指定的限制是 1000。(maxProduct

例如

int[] array = {-5,1,-1,0,10,-10,9,-2,1001};

int maxProduct = arr[0]*arr[2]*arr[4]*arr[5]*arr[6]*arr[7];

【问题讨论】:

  • “有特定限制”是什么意思?无论您指定什么限制,数组的乘积都是数组的乘积。你的意思是元素数量的限制?
  • 我的数组中可能有数字表明他们的产品超过了限制,所以我只想使用那些可能有最大产品的数字。
  • 是限制 maxProduct 的最大值还是数组包含的值?
  • limit 是 maxProduct 的最大值。
  • 通常,好的问题包括自己尝试解决问题

标签: java arrays integer limit product


【解决方案1】:

如果数组有负数,那么你也应该有一个负(最小)值......

您可以在没有可用流的版本中逐个元素地对数组元素进行交互...

int res = 1;
int max = 1000;
for(int i : array){
   res *= i;
   if(res>max) res = max;
}
System.out.println("Product: " +  res);

【讨论】:

  • 我想排除导致最大值为负的负数...
【解决方案2】:
int max = 1000;
int res = 1;
for (int i : array) {
    if (i >= 0)
        res *= i;
    if (res >= max)
        res = max;
}

这将排除负数但接受 0

【讨论】:

  • 如果你将一个数乘以 0 就是 0。:) 你可能需要负数,因为它们可以产生更大的乘积。
  • 我知道。如果您不希望其中包含 0,则应将其添加到您的问题中。你只说没有负数。如果您不想要 0,只需将 if (i >= 0) 更改为 if (i >= 1)
  • 我需要负数,因为两个负数的结果是正数,最大产品可能需要
【解决方案3】:
OptionalInt reduce = Arrays.stream(array).
                     filter(i -> i != 0).
                     reduce((a, b) -> a * b > 1000 ? 1000 : a * b);

System.out.println(reduce.getAsInt());

您可以扩展或删除过滤器。根据您的要求...

【讨论】:

    【解决方案4】:

    如果我猜对了,您正在寻找该数组的一个子集,该子集在给定限制下具有最大乘积,并且输出类似于

    arr[1]*arr[5]*arr[6]*arr[10]
    

    是具有最大值的子集。产品。

    为了做到这一点,您首先需要获取数组的幂集,即数组的所有可能子集,并计算每个子集的乘积,检查它是否是给定限制下的最大值。下面是一个示例:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.List;
    
    public class NewClass {
    
        public static void main(String[] args) {
            Integer[] array = {-5,1,-1,0,10,-10,9,-2,1001};
            List<Integer> list = Arrays.asList(array);
            Integer limit = 1000;
    
            List<List<Integer>> powerset = getPowerset(list);
            List<Integer> maxProdList = getMaxProduct(powerset,limit);
            Integer prod =1;
            for(Integer i : maxProdList){
               prod*=i; 
            }
            System.out.println("List: " + maxProdList );
            System.out.println("max product: " + prod );
        }
    
        //returns all possible subsets [[-5],[-5,1,9],[1,-1,1001][-5,1,-1,0,10] ... and so on]
        // see also http://stackoverflow.com/questions/1670862/obtaining-a-powerset-of-a-set-in-java
        public static List<List<Integer>> getPowerset(Collection<Integer> list) {
            List<List<Integer>> ps = new ArrayList<>();
            ps.add(new ArrayList<>());
            for (Integer item : list) {
              List<List<Integer>> newPs = new ArrayList<>();
    
              for (List<Integer> subset : ps) {
                newPs.add(subset);
                List<Integer> newSubset = new ArrayList<>(subset);
                newSubset.add(item);
                newPs.add(newSubset);
              }
              ps = newPs;
            }
            return ps;
        }
    
        public static List<Integer> getMaxProduct(List<List<Integer>> listOfLists, Integer limit){
            List<Integer> listOfMax = new ArrayList<>();
            Integer max = 1;
            for(List<Integer> list : listOfLists){
                Integer prod =1;
                for(Integer i : list){
                   prod*=i; 
                }
                if(prod>max && prod<=limit){
                    max=prod;
                    listOfMax = list;
                }
            }
            return listOfMax;        
        }
    }
    

    【讨论】:

    • 你好厄立特里亚人,这将花费很多时间..如果我有一个包含 100 个数字的数组怎么办?然后我将不得不检查 100 种可能的方法。谢谢你的帮助。
    【解决方案5】:

    这是一个用伪代码粗略提出的想法。我还没有测试过:

    maxProduct <- 1000
    maxRand <- 99
    lowLimit <- 5
    
    repeat
      num <- random(1, maxRand)
      add num to array
      maxProduct <- maxProduct DIV num
      maxRand <- minimum(maxRand, maxProduct)
    until (maxRand < lowLimit)
    
    if (maxRand > 0) add maxRand to array
    

    我使用 DIV 进行整数除法,并将 maxRand 添加到数组中以使最终乘积更接近原始 maxProduct。您可以根据需要调整 lowLimit。我感到很懒,所以我没有为负数烦恼,可能会选择 2、4 或 6 个数组元素并切换它们的符号。只要负数是偶数,那么最终的结果就是正数。

    【讨论】:

    • 你好,rossum,没关系。整数数组将用作函数的输入,因此我不必“创建”数字。
    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2015-12-16
    • 2019-08-04
    相关资源
    最近更新 更多