Advertisement
Guest User

prob1

a guest
Mar 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin fin
  6. #define cout fout
  7. ifstream fin("prob.in");
  8. ofstream fout("prob.out");
  9.  
  10. int n;
  11. float a[100][100];
  12.  
  13. void out() {
  14.     for (int i = 1; i <= n; i++) {
  15.         for (int j = 1; j <= n; j++)
  16.             cout << setw(7) << a[i][j];
  17.         cout << endl;
  18.     }
  19.     cout << endl;
  20. }
  21.  
  22. float determinant() {
  23.     int flag = 0;
  24.  
  25.     float det = 1;
  26.     for (int i = 1; i <= n; i++)
  27.         for (int k = i + 1; k <= n; k++)
  28.             if (abs(a[i][i]) < abs(a[k][i])) {
  29.                 flag++;
  30.                 for (int j = 1; j <= n; j++)
  31.                     swap(a[i][j], a[k][j]);
  32.             }
  33.     for (int i = 1; i <= n - 1; i++) // loopul principal a lu gauss
  34.         for (int k = i + 1; k <= n; k++) {
  35.             float t = a[k][i] / a[i][i];
  36.             for (int j = 1; j <= n; j++)
  37.                 a[k][j] = a[k][j] - t * a[i][j];
  38.         }
  39.     for (int i = 1; i <= n; i++)
  40.         det = det * a[i][i];
  41.     if (flag % 2 == 0)
  42.         det = det;
  43.     else
  44.         det = -det;
  45.     return det;
  46. }
  47.  
  48. bool prim(int m) {
  49.     if (m < 2) return false;
  50.     for (int i = 2; i < m; i++)
  51.         if (m % i == 0)
  52.             return false;
  53.     return true;
  54. }
  55.  
  56. int main() {
  57.  
  58.     std::vector<int> prime;
  59.  
  60.     cout << "Dati n: "; cin >> n;
  61.     cout << "Dati matricea:\n";
  62.  
  63.     for (int i = 1; i <= n; i++)
  64.         for (int j = 1; j <= n; j++) {
  65.             cin >> a[i][j];
  66.             if (i == j) prime.push_back(a[i][j]);
  67.         }
  68.  
  69.     cout << "Avem matricea:\n"; out();
  70.  
  71.     double det = determinant();
  72.     cout << "Det(A) = " << setw(6) << setprecision(4) << determinant() << endl;
  73.  
  74.     if (det != 0) {
  75.         int index = 0;
  76.         while (index < prime.size()) {
  77.             if (!prim(prime[index])) {
  78.                 prime.erase(prime.begin()+index);
  79.             } else {
  80.                 index++;
  81.             }
  82.         }
  83.         if (prime.size() == 0)
  84.             cout << "Nu avem numere prime pe diagonala principala\n";
  85.         else {
  86.             cout << "Numere prime pe diagonala principala: " << prime.size() << ":" << endl;
  87.             for (auto &it: prime) cout << it << " ";
  88.             cout << endl;
  89.         }
  90.     } else {
  91.         cout << "De la diagonala in jos e zero" << endl;
  92.     }
  93.  
  94.  
  95.  
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement