티스토리 뷰

출처 : https://www.hackerrank.com/challenges/diagonal-difference/problem


#include <bits/stdc++.h>


using namespace std;


int diagonalDifference(vector<vector<int>> arr) {

    int ans1=0; int ans2=0; int s = arr.size();

    for(int i=0,j=0; i>=0 && i<s;i++,j++) {

        ans1 += arr[i][j]; ans2 += arr[i][s-j-1];

    }

    return abs(ans1-ans2);

}


int main()

{

    ofstream fout(getenv("OUTPUT_PATH"));


    int n;

    cin >> n;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');


    vector<vector<int>> arr(n);

    for (int i = 0; i < n; i++) {

        arr[i].resize(n);


        for (int j = 0; j < n; j++) {

            cin >> arr[i][j];

        }


        cin.ignore(numeric_limits<streamsize>::max(), '\n');

    }


    int result = diagonalDifference(arr);


    fout << result << "\n";


    fout.close();


    return 0;

}



'알고리즘 > 기타' 카테고리의 다른 글

[HackerRank] Staircase  (0) 2018.09.27
[HackerRank] Plus Minus  (0) 2018.09.27
[HackerRank] A Very Big Sum  (0) 2018.09.27
[HackerRank] Compare the Triplets  (0) 2018.09.27
[HackerRank] Simple Array Sum  (0) 2018.09.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함