英文:
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 < arr[i].length - 1; i++) {
total = 0;
for (int j = 0; j < 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
答案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 = "The row numbers where the condition is true are: ";
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论