【问题标题】:Finding the first pair of maximal sum by indices in Java在Java中通过索引查找第一对最大和
【发布时间】:2021-01-18 01:03:46
【问题描述】:

我有一个代码,我必须在其中打印两个相同长度的数组之间最大和的第一对索引。来自 arr1 的第一个值 i 和来自 arr2 的第二个值 j。我成功地找到了最大值为 7 的值对及其索引(有两对总和为 7)。但我只需要打印最接近数组第一个元素的最大和的第一对。

import java.util.ArrayList;
import java.util.List;

class maximal_sum
{
    static void kLargestPair(int[] arr1, int n1, int[] arr2, int n2, int k)
    {
        if (k > n1*n2)
        {
            System.out.print("k pairs don't exist");
            return ;
        }

        int index2[] = new int[n1];

        while (k > 0)
        {
            int max_sum = Integer.MIN_VALUE;
            int max_index = 0;

            for (int i1 = 0; i1 < n1; i1++)
            {
                if (index2[i1] < n2 &&
                        arr1[i1] + arr2[index2[i1]] > max_sum)
                {
                    max_index = i1;

                    max_sum = arr1[i1] + arr2[index2[i1]];
                    List<int[]> result = new ArrayList<int[]>();

                    result.add(new int[]{arr1[max_index],arr2[index2[max_index]]});

                    if(index2[max_index] > max_index) {

                        System.out.print("(" + arr1[max_index] + ", " +
                                arr2[index2[max_index]] + ") "); 
                        //here prints the pair of values: (6,1) (4,3)
                        //we just need to print (4,3) because it comes before (6,1) according to the indices

                        System.out.print("(" + max_index + ", " +
                                index2[max_index] + ") ");
                       //here prints the pair of indices: (2,3) (0,1)
                       //we just need to print (0,1) because it comes before (2,3) according to the indices

                    }


                }
            }

            index2[max_index]++;
            k--;
        }
    }

    public static void main (String[] args)
    {
        int[] arr1 = {4, -8, 6, 0};
        int n1 = arr1.length;

        int[] arr2 = {-10 ,3, 1, 1};
        int n2 = arr2.length;

        int k = 6;
        kLargestPair( arr1, n1, arr2, n2, k);
    }
}

【问题讨论】:

    标签: java arrays list arraylist indices


    【解决方案1】:

    我不确定为什么要在循环时打印出最大值以及为什么要循环六次(k = 6)而不是四次(即数组的长度)。我不明白你使用k,它的意图是什么?

    为了更好地构建代码,首先循环遍历数组之间的每个可能的数字组合并计算总和。存储对最大总和和相关索引的引用。计算后简单打印与索引的最大和。这必须在所有计算完成后完成。

    在代码中:

    static void kLargestPair(int[] arr1, int[] arr2) {
       int biggestSum = Integer.MIN_VALUE; //storing reference to biggest sum
       int biggestIndexArr1; //storing reference to biggest index in arr1
       int biggestIndexArr2; //storing reference to biggest index in arr2
       int currentSum; //stroing reference to current calculated sum for the current calculation
    
       for(int indexArr1 = 0; indexArr1 < arr1.length; indexArr1++) { //Nested for loops to calculate every possible sum in the two arrays
          for(int indexArr2 = 0; indexArr2 < arr2.length; indexArr2++) {
             currentSum = arr1[indexArr1] + arr2[indexArr2]; //calculate current sum
             if(currentSum > biggestSum) { //By only checking > instead of >= only the first encounter of the biggest sum will be stored (getting a sum as big as the current biggest sum will not store)
                 biggestSum = currentSum;
                 biggestIndexArr1 = indexArr1;
                 biggestIndexArr2 = indexArr2;
             }
          }
       } 
       System.out.println("Biggest sum is " + biggestSum + " at index (" + biggestIndexArr1 + "," + biggestIndexArr2 + ")");
    }
    
    public static void main (String[] args)
        {
            int[] arr1 = {4, -8, 6, 0};
            int[] arr2 = {-10 ,3, 1, 1};   
            kLargestPair( arr1, arr2);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多