Advertisement
Spocoman

02. Primary Diagonal

Jan 26th, 2024
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n, el;
  8.     cin >> n;
  9.  
  10.     vector<vector<int>> matrix;
  11.  
  12.     for (int i = 0; i < n; i++) {
  13.         vector<int> currentRow;
  14.         for (int j = 0; j < n; j++) {
  15.             cin >> el;
  16.             currentRow.push_back(el);
  17.         }
  18.         matrix.push_back(currentRow);
  19.     }
  20.  
  21.     int sum = 0;
  22.  
  23.     for (int i = 0; i < n; i++) {
  24.         sum += matrix[i][i];
  25.     }
  26.  
  27.     cout << sum << endl;
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement