Matrix Diagonal Sum
Submit solution
Points:
10
Time limit:
1.0s
Memory limit:
10M
Authors:
Problem type
Allowed languages
Java 19
給定一個整數 n,代表矩陣大小為 n×n,接著輸入 n 行、每行 n 個整數作為矩陣內容,請使用二維陣列計算並輸出主對角線與副對角線元素的總和。
Sample Input / Output
| Sample 1 | Sample 2 | Sample 3 | |
|---|---|---|---|
| Input | 3 1 2 3 4 5 6 7 8 9 |
2 10 20 30 41 |
4 1 0 0 1 0 2 2 0 0 3 3 0 4 0 0 4 |
| 主對角線 | \[1+5+9=15\] | \[10+41=51\] | \[1+2+3+4=10\] |
| 副對角線 | \[3+5+7=15\] | \[20+30=50\] | \[1+2+3+4=10\] |
| Output | 15 15 | 51 50 | 10 10 |
Comments