# 对角线差异

##### 问题描述投票：2回答：4

`````` public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int j = 0;
int a[][] = new int[n][n];
int count1 = 0; int count2 = 0;
int result = 0;
for(int a_i=0; a_i < n; a_i++){
for(int a_j = 0;a_j < n;a_j++){

a[a_i][j] = in.nextInt();

}}
for(int i = 0;i<n;i++)
{
count1+=a[i][j];
System.out.println(count1);
j++;
}
j = 0;
for(int i = n-1;i>=0;i--)
{
count2+=a[i][j];
System.out.println(count2);
j++;
}
result = Math.abs(count1 - count2);
System.out.println(result);
}
``````

java arrays for-loop
##### 4个回答
2

``````for(int i = 0;i<n;i++){
count1+=a[i][i];
count2+=a[i][n-1-i];
}
``````

0

``````for(int i=0; i < n; i++){
for(int j = 0; j < n; j++){
int v = in.nextInt();
count1 += (i == j) ? v : 0;
count2 += (i + j == n-1) ? v : 0;
}
}
``````

0

``````    int result = 0;
for(int i = 0;i<n;i++){
l+=a[i][i];
r+=a[i][n-1-i];
}
result = abs(l-r);
``````

``````def diagonalDifference(a):
l = sum(a[i][i] for i in range(len(a)))
r = sum(a[i][len(a)-1-i] for i in range(len(a)))
return (abs(r- l))
``````

-1

``````int k=0;
int l=0;
int m;
int z=arr.length;
int s=arr.length-1;
for(int i=0;i<z;i++) {
for(int j=0;j<z;j++) {
if(i==j){
k=k+arr[i][j];
}
}
l=l+arr[i][s];
s--;
}
if(l>k) {
m=l-k;
} else {
m=k-l;
}
return m;
``````