Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.
Input Format
The first line contains a single integer, . The next lines denote the matrix's rows, with each line containing space-separated integers describing the columns.
Constraints
Output Format
Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int a[][] = new int[n][n]; for(int a_i=0; a_i < n; a_i++){ for(int a_j=0; a_j < n; a_j++){ a[a_i][a_j] = in.nextInt(); } } int answer = 0; int first = 0; int second = 0; for (int i = 0; i < n; i++) { first += a[i][i]; second += a[(n - 1) - i][i]; } answer = Math.abs(first-second); System.out.println(answer); } }
'Programming > >> Algorithm' 카테고리의 다른 글
[Lucky Algorithm] Staircase (0) | 2017.10.03 |
---|---|
[Lucky Algorithm] Plus Minus (0) | 2017.10.03 |
[Lucky Algorithm] A Very Big Sum (0) | 2017.10.03 |
[Lucky Algorithm] Compare the Triplets (0) | 2017.10.03 |
[Lucky Algorithm] Simple Array Sum (0) | 2017.10.03 |
댓글