Skip to Content
CoursesCSC102การหาผลต่างของผลรวมเส้นทแยงมุมในเมทริกซ์จัตุรัส

การหาผลต่างของผลรวมเส้นทแยงมุมในเมทริกซ์จัตุรัส

โจทย์

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, nn, the number of rows and columns in the square matrix (the matrix is n×nn \times n). The next nn lines each contain nn 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: 11+512=411 + 5 − 12 = 4.
  • The secondary diagonal is: 4, 5, 10. Sum across the secondary diagonal: 4+5+10=194 + 5 + 10 = 19.
  • Difference: 419=15| 4 − 19 | = 15.
InputOutput
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 มิติขนาด size×sizesize \times size
  • ใช้ 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() เพื่อหาค่าสัมบูรณ์ของผลต่าง

แผนภาพการทำงาน

ตัวอย่างการทำงาน

สมมติเมทริกซ์ขนาด 3×33 \times 3:

11 2 4 4 5 6 10 8 -12

การคำนวณ:

  1. เส้นทแยงมุมหลัก (↘):
  • [0,0]: 11
  • [1,1]: 5
  • [2,2]: -12
  • ผลรวม = 11+5+(12)=411 + 5 + (-12) = 4
  1. เส้นทแยงมุมรอง (↙):
  • [0,2]: 4
  • [1,1]: 5
  • [2,0]: 10
  • ผลรวม = 4+5+10=194 + 5 + 10 = 19
  1. ผลต่างสัมบูรณ์:
  • 419=15|4 - 19| = 15

การหาตำแหน่งในเส้นทแยงมุม

เมทริกซ์ขนาด 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]
ปรับปรุงล่าสุด