Advertisement
Spocoman

02. The Matrix

Feb 5th, 2024
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime(int n) {
  8.     for (int i = 2; i <= sqrt(n); i++) {
  9.         if (n % i == 0) {
  10.             return false;
  11.         }
  12.     }
  13.     return true;
  14. }
  15.  
  16. int main() {
  17.     int n;
  18.     cin >> n;
  19.  
  20.     vector<vector<int>> numbers(n, vector<int>(n));
  21.  
  22.     for (int r = 0; r < n; r++) {
  23.         for (int c = 0; c < n; c++) {
  24.             cin >> numbers[r][c];
  25.         }
  26.     }
  27.  
  28.     int startCol = 1, finalCol = n - 1, sum = 0;
  29.  
  30.     for (int r = n - 1; r > n / 2; r--) {
  31.         for (int c = startCol; c < finalCol; c++) {
  32.             if (isPrime(numbers[r][c])) {
  33.                 sum += numbers[r][c];
  34.             }
  35.         }
  36.         startCol++;
  37.         finalCol--;
  38.     }
  39.  
  40.     cout << sum << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement