4DM3M

Возведение квадратной матрицы в степень

Dec 27th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void identity(int N)
  5. {
  6.     int i, j;
  7.     int a[10][10];
  8.     for (i = 0; i < N; i++)
  9.     {
  10.         for (j = 0; j < N; j++)
  11.         {
  12.             if (i == j) a[i][j] = 1;
  13.             else a[i][j] = 0;
  14.             cout << a[i][j] << " ";
  15.         }
  16.         cout << endl;
  17.     }
  18. }
  19.  
  20. void main()
  21. {
  22.     setlocale(LC_ALL, "");
  23.     const int nn = 10;
  24.     int a[nn][nn], b[nn][nn], c[nn][nn];
  25.     int n, p, k, i, j, st;
  26.     cout << "Введите ранг матрицы: ";
  27.     cin >> n;
  28.  
  29.     cout << "Введите матрицу:\n";
  30.     for (i = 0; i < n; i++)
  31.         for ( j = 0; j < n; j++)
  32.             cin >> a[i][j];                 //Ввод матрицы A
  33.     cout << endl;
  34.    
  35.     for (i = 0; i < n; i++)
  36.         for (j = 0; j < n; j++) b[i][j] = a[i][j];  //Создание матрицы B
  37.  
  38.     cout << "Введите степень: ";
  39.     cin >> st;
  40.  
  41.     switch (st)
  42.     {
  43.     case 0:
  44.         identity(n);
  45.         break;
  46.  
  47.     case 1:
  48.         for (i = 0; i < n; i++)
  49.         {
  50.             for (j = 0; j < n; j++) cout << a[i][j] << " ";
  51.             cout << endl;
  52.         }
  53.         break;
  54.  
  55.     default:
  56.         while (--st != 0)
  57.         {
  58.             for (i = 0; i < n; i++)
  59.                 for (j = 0; j < n; j++)
  60.                 {
  61.                     int s = 0;
  62.                     for (int k = 0; k < n; k++)
  63.                         s += a[i][k] * b[k][j];
  64.                     c[i][j] = s;
  65.                 }
  66.  
  67.             for (i = 0; i < n; i++)
  68.                 for (j = 0; j < n; j++) a[i][j] = c[i][j];
  69.         }
  70.  
  71.         for (i = 0; i < n; i++)
  72.         {
  73.             for (j = 0; j < n; j++) cout << a[i][j] << " ";
  74.             cout << endl;
  75.         }
  76.         break;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment