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

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

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

问题

  1. public class TwoDArray {
  2. public static void SquareNum(final int[][] arr) {
  3. for (int i = 0; i < arr.length; i++) {
  4. int squareSum = 0;
  5. int diagonalValue = arr[i][i];
  6. for (int j = 0; j < arr[i].length; j++) {
  7. if (j != i) {
  8. squareSum += arr[i][j];
  9. }
  10. }
  11. int calculatedSquare = squareSum * squareSum;
  12. if (calculatedSquare == diagonalValue) {
  13. System.out.println(diagonalValue + " is valid.");
  14. } else {
  15. System.out.println(diagonalValue + " is not valid.");
  16. }
  17. }
  18. }
  19. public static void main(final String[] args) {
  20. final int[][] arr = {{ 144, 2, 3, 2, 5},
  21. {2, 36, 1, 2, 1},
  22. {0, 0, 9, 0, 3},
  23. {4, 4, 4, 225, 3},
  24. {1, 1, 1, 1, 16}};
  25. SquareNum(arr);
  26. }
  27. }
英文:

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

Say, this is my array:

  1. int[][] arr = {{ 144, 2, 3, 2, 5},
  2. {2, 36, 1, 2, 1},
  3. {0, 0, 9, 0, 3},
  4. {4, 4, 4, 225, 3},
  5. {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.

  1. public class TwoDArray {
  2. public static void SquareNum(final int[][] arr) {
  3. double sqaureSum = 0;
  4. int total = 0;
  5. for (int i = 0; i &lt; arr[i].length - 1; i++) {
  6. total = 0;
  7. for (int j = 0; j &lt; arr[i].length; j++) {
  8. }
  9. ???
  10. }
  11. }
  12. public static void main(final String[] args) {
  13. final int[][] arr = {{ 144, 2, 3, 2, 5},
  14. {2, 36, 1, 2, 1},
  15. {0, 0, 9, 0, 3},
  16. {4, 4, 4, 225, 3},
  17. {1, 1, 1, 1, 16}};
  18. SquareNum(arr);
  19. }
  20. }

Any leads on how I can achieve this?

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

答案1

得分: 1

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

  1. class TwoDArray {
  2. public static void SquareNum(final int[][] arr) {
  3. String output = "满足条件的行数为:";
  4. int total = 0;
  5. for (int i = 0; i < arr[i].length; i++) {
  6. total = 0;
  7. for (int j = 0; j < arr[i].length; j++) {
  8. total += arr[i][j];
  9. }
  10. double squareSum = Math.pow(total - arr[i][i], 2);
  11. if (squareSum == arr[i][i]) {
  12. output += i + " ";
  13. }
  14. }
  15. System.out.println(output);
  16. }
  17. public static void main(final String[] args) {
  18. final int[][] arr = {{ 144, 2, 3, 2, 5},
  19. {2, 36, 1, 2, 1},
  20. {0, 0, 9, 0, 3},
  21. {4, 4, 4, 225, 3},
  22. {1, 1, 1, 1, 16}};
  23. SquareNum(arr);
  24. }
  25. }
英文:

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.

  1. class TwoDArray {
  2. public static void SquareNum(final int[][] arr) {
  3. String output = &quot;The row numbers where the condition is true are: &quot;;
  4. int total = 0;
  5. for (int i = 0; i &lt; arr[i].length; i++) {
  6. total = 0;
  7. for (int j = 0; j &lt; arr[i].length; j++) {
  8. total += arr[i][j];
  9. }
  10. double squareSum = Math.pow(total - arr[i][i], 2);
  11. if (squareSum == arr[i][i])
  12. {
  13. output += i + &quot; &quot;;
  14. }
  15. }
  16. System.out.println(output);
  17. }
  18. public static void main(final String[] args) {
  19. final int[][] arr = {{ 144, 2, 3, 2, 5},
  20. {2, 36, 1, 2, 1},
  21. {0, 0, 9, 0, 3},
  22. {4, 4, 4, 225, 3},
  23. {1, 1, 1, 1, 16}};
  24. SquareNum(arr);
  25. }
  26. }

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:

确定