การหาผลต่างของผลรวมเส้นทแยงมุมในเมทริกซ์จัตุรัส
โจทย์
You are given a square matrix of integers. Your task is to calculate the absolute difference between the sums of its primary diagonal (from the top-left to the bottom-right) and its secondary diagonal (from the top-right to the bottom-left).
Input:
- The first line contains a single integer, , the number of rows and columns in the square matrix (the matrix is ). The next lines each contain space-separated integers representing the rows of the matrix.
Output:
- Return a single integer representing the absolute difference between the sums of the matrix’s two diagonals.
Explanation:
- The primary diagonal is: 11, 5, -12. Sum across the primary diagonal: .
- The secondary diagonal is: 4, 5, 10. Sum across the secondary diagonal: .
- Difference: .
| Input | Output |
|---|---|
| 3 11 2 4 4 5 6 10 8 -12 | 15 |
โค้ด
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = scanner.nextInt();
}
}
int primaryDiagonalSum = 0;
int secondaryDiagonalSum = 0;
for (int i = 0; i < size; i++) {
primaryDiagonalSum += matrix[i][i];
secondaryDiagonalSum += matrix[i][size - 1 - i];
}
int difference = Math.abs(primaryDiagonalSum - secondaryDiagonalSum);
System.out.println(difference);
}
}คำอธิบาย
1. การประกาศคลาสและรับขนาดเมทริกซ์
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt(); // ขนาดของเมทริกซ์จัตุรัส2. การสร้างและรับข้อมูลเมทริกซ์
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = scanner.nextInt();
}
}- สร้างเมทริกซ์ 2 มิติขนาด
- ใช้ loop ซ้อนเพื่อรับค่าข้อมูลทีละตำแหน่ง
3. การคำนวณผลรวมเส้นทแยงมุม
int primaryDiagonalSum = 0; // ผลรวมเส้นทแยงมุมหลัก
int secondaryDiagonalSum = 0; // ผลรวมเส้นทแยงมุมรอง
for (int i = 0; i < size; i++) {
primaryDiagonalSum += matrix[i][i]; // เส้นทแยงมุมหลัก [0,0], [1,1], [2,2], ...
secondaryDiagonalSum += matrix[i][size - 1 - i]; // เส้นทแยงมุมรอง [0,2], [1,1], [2,0], ...
}- คำนวณผลรวมทั้งสองเส้นทแยงมุมในลูปเดียว
- เส้นทแยงมุมหลัก: แถวและคอลัมน์เท่ากัน (i,i)
- เส้นทแยงมุมรอง: แถว i และคอลัมน์ (size-1-i)
4. การคำนวณผลต่างสัมบูรณ์และแสดงผล
int difference = Math.abs(primaryDiagonalSum - secondaryDiagonalSum);
System.out.println(difference);- ใช้
Math.abs()เพื่อหาค่าสัมบูรณ์ของผลต่าง
แผนภาพการทำงาน
ตัวอย่างการทำงาน
สมมติเมทริกซ์ขนาด :
11 2 4
4 5 6
10 8 -12การคำนวณ:
- เส้นทแยงมุมหลัก (↘):
- [0,0]: 11
- [1,1]: 5
- [2,2]: -12
- ผลรวม =
- เส้นทแยงมุมรอง (↙):
- [0,2]: 4
- [1,1]: 5
- [2,0]: 10
- ผลรวม =
- ผลต่างสัมบูรณ์:
การหาตำแหน่งในเส้นทแยงมุม
เมทริกซ์ขนาด 3×3:
[0,0] [0,1] [0,2]
[1,0] [1,1] [1,2]
[2,0] [2,1] [2,2]
เส้นทแยงมุมหลัก (↘): เส้นทแยงมุมรอง (↙):
[0,0] [0,2]
[1,1] [1,1]
[2,2] [2,0]ปรับปรุงล่าสุด