2D数组求每行除了对角线索引以外的所有数字之和。

huangapple go评论70阅读模式
英文:

2D Array sum all numbers in a row except for the diagonal index

问题

public class TwoDArray {

    public static void SquareNum(final int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            int squareSum = 0;
            int diagonalValue = arr[i][i];
            
            for (int j = 0; j < arr[i].length; j++) {
                if (j != i) {
                    squareSum += arr[i][j];
                }
            }
            
            int calculatedSquare = squareSum * squareSum;
            
            if (calculatedSquare == diagonalValue) {
                System.out.println(diagonalValue + " is valid.");
            } else {
                System.out.println(diagonalValue + " is not valid.");
            }
        }
    }
    
    public static void main(final String[] args) {
        final int[][] arr =   {{ 144, 2, 3, 2, 5},
                            {2, 36, 1, 2, 1},
                            {0, 0, 9, 0, 3},
                            {4, 4, 4, 225, 3},
                            {1, 1, 1, 1, 16}};
        SquareNum(arr);
    }
}
英文:

I was wrecking my brain for a while now and finally gave up.

Say, this is my array:

   int[][] arr =   {{ 144, 2, 3, 2, 5},
                    {2, 36, 1, 2, 1},
                    {0, 0, 9, 0, 3},
                    {4, 4, 4, 225, 3},
                    {1, 1, 1, 1, 16}};

I need to calculate the sum of all of the numbers except for the arr[i][i] and then compare it's power of two to the said number.

For example, 144 is the arr[0][0] in the first loop. I need to take (2 + 3 + 2 + 5)^2 and check if it's equal to 144 (it is). I was happy when I did it with the first row, but then got stuck on idea of taking every now except the arr[i][i] number, which in the second row is 36.

I got nothing until now, only wrong assumptions. I'm still learning 2D arrays but the subject doesn't sit well.

public class TwoDArray {

    public static void SquareNum(final int[][] arr) {
    double sqaureSum = 0;
    int total = 0;
    for (int i = 0; i &lt; arr[i].length - 1; i++) {
        total = 0;
        for (int j = 0; j &lt; arr[i].length; j++) {
            }
            ???
        }
    }
    public static void main(final String[] args) {
        final int[][] arr =   {{ 144, 2, 3, 2, 5},
                            {2, 36, 1, 2, 1},
                            {0, 0, 9, 0, 3},
                            {4, 4, 4, 225, 3},
                            {1, 1, 1, 1, 16}};
    SquareNum(arr);
    }
}

Any leads on how I can achieve this?

Thanks 2D数组求每行除了对角线索引以外的所有数字之和。

答案1

得分: 1

以下是已经翻译好的代码部分:

class TwoDArray {

    public static void SquareNum(final int[][] arr) {
        String output = "满足条件的行数为:";
        int total = 0;
        for (int i = 0; i < arr[i].length; i++) {
            total = 0;
            for (int j = 0; j < arr[i].length; j++) {
                total += arr[i][j];
            }
            double squareSum = Math.pow(total - arr[i][i], 2);
            if (squareSum == arr[i][i]) {
                output += i + " ";
            }
        }
        System.out.println(output);
    }
    
    public static void main(final String[] args) {
        final int[][] arr = {{ 144, 2, 3, 2, 5},
                             {2, 36, 1, 2, 1},
                             {0, 0, 9, 0, 3},
                             {4, 4, 4, 225, 3},
                             {1, 1, 1, 1, 16}};
        SquareNum(arr);
    }
}
英文:

Here's the code with my suggestions in the comments above added. I've also shown how I'd do the output as you described.

class TwoDArray {

    public static void SquareNum(final int[][] arr) {
        String output = &quot;The row numbers where the condition is true are: &quot;;
        int total = 0;
        for (int i = 0; i &lt; arr[i].length; i++) {
            total = 0;
            for (int j = 0; j &lt; arr[i].length; j++) {
                total += arr[i][j];
            }
            double squareSum = Math.pow(total - arr[i][i], 2);
            if (squareSum == arr[i][i])
            {
                output += i + &quot; &quot;;
            }
        }
        System.out.println(output);
    }
    public static void main(final String[] args) {
        final int[][] arr =   {{ 144, 2, 3, 2, 5},
                {2, 36, 1, 2, 1},
                {0, 0, 9, 0, 3},
                {4, 4, 4, 225, 3},
                {1, 1, 1, 1, 16}};
        SquareNum(arr);
    }
}

huangapple
  • 本文由 发表于 2020年4月8日 01:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61086249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定