Advertisement
Seal_of_approval

massiv

Dec 18th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int** iter(int** a, int** b, int n)
  9. {
  10. int** res = new int*[n];
  11. for (int i = 0; i < n; i++)
  12. {
  13. res[i] = new int[n];
  14. for (int j = 0; j<n; j++)
  15. res[i][j] = 0;
  16. }
  17.  
  18. for (int row = 0; row < n; row++)
  19. for (int col = 0; col < n; col++)
  20. for (int inner = 0; inner < n; inner++)
  21. res[row][col] += a[row][inner] * b[inner][col];
  22.  
  23. return res;
  24. }
  25.  
  26.  
  27. int** power(int** a, int** b, int n, int s)
  28. {
  29.  
  30. int** res = new int*[n];
  31. for (int i = 0; i < n; i++)
  32. {
  33. res[i] = new int[n];
  34. for (int j = 0; j < n; j++)
  35. res[i][j] = 0;
  36. }
  37.  
  38. res = iter(a, b, n);
  39.  
  40. if (s == 1) return res;
  41.  
  42. for (int i = 2; i < s; i++)
  43. {
  44. res = iter(res, b, n);
  45. }
  46.  
  47. return res;
  48. }
  49.  
  50.  
  51. int main(void)
  52. {
  53. freopen("input.txt", "r", stdin);
  54. freopen("output.txt", "w", stdout);
  55.  
  56. int n;
  57. cin >> n;
  58.  
  59. int** a = new int *[n];
  60. int** b = new int *[n];
  61. int** r = new int *[n];
  62. for (int i = 0; i < n; i++)
  63. {
  64. a[i] = new int[n];
  65. b[i] = new int[n];
  66. r[i] = new int[n];
  67. }
  68.  
  69. int s;
  70. cin >> s;
  71. for (int i = 0; i < n; i++)
  72. for (int j = 0; j < n; j++)
  73. {
  74. cin >> a[i][j];
  75. b[i][j] = a[i][j];
  76. r[i][j] = 0;
  77. }
  78.  
  79. r = power(a, b, n, s);
  80.  
  81. for (int i = 0; i < n; i++)
  82. {
  83. for (int j = 0; j < n; j++)
  84. cout << r[i][j] << " ";
  85. cout << endl;
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement