2020.4.28
大晚上不容易啊,差点就忘了,学习的第七天
求3 x 3 矩阵的主对角线的和与副对角线的和
Java代码
import java.util.Scanner;
public class Matrix_Diagonal_Add {
public static void main(String[] args) {
int[][] a = new int[3][3];
int sum_1 = 0, sum_2 = 0;
Scanner input = new Scanner(System.in);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = input.nextInt();//输入矩阵
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (j == i)//判断是否是主对角线
sum_1 += a[i][j];
if (j == a[i].length - i - 1)//判断是否是副对角线
sum_2 += a[i][j];
}
}
System.out.println(sum_1 + " " + sum_2);
}
}
C也没有很大的区别,今天偷个懒~~
如有错误恳请指正,感激不尽,又是愉快的一天结束
算法题目来源:https://www.dotcpp.com/