资讯专栏INFORMATION COLUMN

[HackerRank] Diagonal Difference

warmcheng / 2253人阅读

Problem

Given a square matrix of size N x N, calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, N. The next N lines denote the matrix"s rows, with each line containing N space-separated integers describing the columns.

Output Format

Print the absolute difference between the two sums of the matrix"s diagonals as a single integer.

Sample Input

3

11    2     4
4     5     6
10    8     -12

Sample Output

15

Explanation

The primary diagonal is:

11
      5
            -12

Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is:

            4
      5
10

Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15

Solution

import java.io.*;
import java.util.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int pSum = 0, sSum = 0;
        int a[][] = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = in.nextInt();
            }
        }
        for(int i=0; i           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66019.html

相关文章

  • Hackerrank Practice

    摘要:数组中重复元素的个数题目找到一个数组中重复元素的个数。能否成为题目互减,直到的时候。此方法精彩此方法更精彩找两边相等的 Anagram 拆分数组 看一半操作几次能成为另一半的anagram 题目 输入第一行是要判断的字符串的个数n,之后的n行输入为需要判断的字符串。每个字符串str,是两个等长字符串的合体,所以长度一定是偶数。若为奇数,返回-1。所以str分成两个substring,右...

    arashicage 评论0 收藏0
  • [HackerRank] Simple Array Sum | A Very Big Sum

    Simple Array Sum Problem Given an array of N integers, can you find the sum of its elements? Input Format The first line contains an integer, N, denoting the size of the array. The second line contain...

    harriszh 评论0 收藏0
  • [HackerRank] Plus Minus

    Problem Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Pri...

    leeon 评论0 收藏0
  • [HackerRank] Staircase

    Problem Your teacher has given you the task of drawing a staircase structure. Being an expert programmer, you decided to make a program to draw it for you instead. Given the required height, can you p...

    explorer_ddf 评论0 收藏0
  • [HackerRank] Time Conversion

    Problem Given a time in AM/PM format, convert it to military (24-hour) time. Input Format A single string containing a time in 12-hour clock format. Output Format Convert and print the given time in 2...

    longmon 评论0 收藏0

发表评论

0条评论

warmcheng

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<