简体   繁体   English

检查整数数组中是否存在一行,该行是同一数组中其他两行的总和

[英]Check if there exist a row in 2D array of integers that is the sum of two other rows of the same array

I am trying to figure out how can I code a program that returns true if there exist a row in a 2D array which is the sum of two other rows. 我试图弄清楚如何编写一个程序,如果2D数组中存在一行,而该行是其他两行的总和,那么该程序将返回true。

example of what my program should do: 我的程序应该执行的示例:

if the 2D array is: 如果2D数组是:

2 4 2 3 2 4 2 3

3 2 6 1 3 2 6 1

5 6 8 4 5 6 8 4

9 7 3 7 9 7 3 7

my code should return true because row[2] (3rd row) is the sum of row[0] (1st row) and row[1] (2nd row) 我的代码应该返回true,因为row [2](第三行)是row [0](第一行)和row [1](第二行)的总和

In my code, I search in the first column of my 2D array to find the location of a value that is the sum of two other values from different rows, but cannot figure out what to do after that. 在我的代码中,我在2D数组的第一列中进行搜索,以找到一个值的位置,该值是来自不同行的两个其他值的总和,但无法弄清楚该怎么做。

    boolean someRowIsSumOfTwoOthers(int n, int [][] A){ 
        int i, j, k;
        boolean isTotal = false;
        for( i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                if (i != j){        
                    for( k = 0; k < n; k++){
                       if ( (i != k) && (j != k) )  
                            if( A[i][0] == A[j][0] + A[k][0] )
                                isTotal = true;
                                 //once isTotal true, I need to increment column to check if isTotal still true for the rest of that same row
                    }
                }
            }

            if ( (i == n) && isTotal )
                return true;            
        }

        return false;                   
    }

For clarity I feel it'd be worth splitting the problem into two parts: 为了清楚起见,我认为将问题分为两个部分是值得的:

boolean rowIsSumOfTwoOthers(int[][] table) {
    int[] sums = sumOfRows(table);
    return oneIsSumOfTwoOthers(sums);
}

Each of these is relatively simple using Java 8 streams: 使用Java 8流,每种方法都相对简单:

private int[] sumOfRows(int[][] table) {
    return IntStream.range(0, table.length)
        .mapToInt(row -> Arrays.stream(table[row]).sum()).toArray();
}

And: 和:

private boolean oneIsSumOfTwoOthers(int[] sums) {
    return IntStream.range(0, sums.length)
        .anyMatch(s1 ->
            IntStream.range(0, sums.length)
            .filter(s2 -> s2 != s1)
            .anyMatch(s2 ->
                IntStream.range(0, sums.length)
                .filter(s3 -> s3 != s1 && s3 != s2)
                .anyMatch(s3 -> sums[s1] == sums[s2] + sums[s3])));
}

I think this will give you the desired answer 我想这会给你想要的答案

boolean someRowIsSumOfTwoOthers(int n, int [][] A){ 
    int i, j=0;
    int []rowSums=new int[n];
    boolean isTotal = false;
    //This part cut the code into one deminsion
    //Makes it eaiser to work with
    for(int[] b:A)
    {
        for(i=0;i<b.length;i++)
        {
            rowSums[j]+=b[i];
        }
        //useing j as a row incrimenter
        j++;
    }
    //for the value of one row
    int rowVal;
    //for the value of the other two
    for(int x=0;x<n;x++)
    {
        rowVal=rowSums[x];
        //nested forloop to test any of the other rows combinations
        for(int y=0;y<n;y++)
        {
            //check to keep row values from repeating
            if(y==x)
                y++;
            for(int z=0;z<n;z++)
            {
                //check to keep row values from repeating
                while(z==y || z==x)
                    z++;
                if(z>=n)
                    break;
                if(rowVal==rowSums[z]+rowSums[y])
                    return true;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM