1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include<iostream> #include<algorithm> #include<iomanip> #include<time.h> using namespace std; const int N = 8; int A[N][N], B[N][N], C[N][N];
void input(int n, int X[][N]) { srand((unsigned)time(NULL)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int x = rand() % 9 + 1; X[i][j] = x; } } }
void output(int n, int X[][N]) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cout << setw(3) << setfill(' ') << X[i][j] << ' '; } cout << endl; cout << endl; } }
void Matrix_Multiply(int A[][N], int V[][N], int C[][N]) { for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { C[i][j] = 0; for(int k = 0; k < 2; k++) { C[i][j] = C[i][j] + A[i][k] * B[k][j]; } } } }
void Matrix_Add(int n , int A[][N], int B[][N], int C[][N]) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { C[i][j] = A[i][j] + B[i][j]; } } }
void Matrix_Sub(int n , int A[][N], int B[][N], int C[][N]) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { C[i][j] = A[i][j] - B[i][j]; } } }
void Strassen(int n,int A[][N], int B[][N], int C[][N]) { int A11[N][N], A12[N][N], A21[N][N], A22[N][N]; int B11[N][N], B12[N][N], B21[N][N], B22[N][N]; int C11[N][N], C12[N][N], C21[N][N], C22[N][N]; int M1[N][N], M2[N][N], M3[N][N], M4[N][N], M5[N][N], M6[N][N], M7[N][N]; int AA[N][N], BB[N][N], MM1[N][N], MM2[N][N]; if(n == 2) { Matrix_Multiply(A, B, C); }else { for(int i = 0; i < n / 2; i++) { for(int j = 0; j < n / 2; j++) { A11[i][j] = A[i][j]; A12[i][j] = A[i][j + n / 2]; A21[i][j] = A[i + n / 2][j]; A22[i][j] = A[i + n / 2][j + n / 2]; B11[i][j] = A[i][j]; B12[i][j] = A[i][j + n / 2]; B21[i][j] = A[i + n / 2][j]; B22[i][j] = A[i + n / 2][j + n / 2]; } } Matrix_Sub(n / 2, B12, B22, BB); Strassen(n / 2, A11, BB, M1); Matrix_Add(n / 2, A11, A12, AA); Strassen(n / 2, AA, B22, M2); Matrix_Add(n / 2, A21, A22, AA); Strassen(n / 2, AA, B11, M3); Matrix_Sub(n / 2, B21, B11, BB); Strassen(n / 2, A22, BB, M4); Matrix_Add(n / 2, A11, A22, AA); Matrix_Add(n / 2, B11, B22, BB); Strassen(n / 2, AA, BB, M5); Matrix_Sub(n / 2, A12, A22, AA); Matrix_Add(n / 2, B21, B22, BB); Strassen(n / 2, AA, BB, M6); Matrix_Sub(n / 2, A11, A21, AA); Matrix_Add(n / 2, B11, B12, BB); Strassen(n / 2, AA, BB, M7); Matrix_Add(N / 2, M5, M4, MM1); Matrix_Sub(N / 2, M2, M6, MM2); Matrix_Sub(N / 2, MM1, MM2, C11); Matrix_Add(N / 2, M1, M2, C12); Matrix_Add(N / 2, M3, M4, C21); Matrix_Add(N / 2, M5, M1, MM1); Matrix_Add(N / 2, M3, M7, MM2); Matrix_Sub(N / 2, MM1, MM2, C22); for(int i = 0; i < n / 2; i++) { for(int j = 0; j < n / 2; j++) { C[i][j] = C11[i][j]; C[i][j + n / 2] = C12[i][j]; C[i + n / 2][j] = C21[i][j]; C[i + n / 2][j + n / 2] = C22[i][j]; } } } } int main() { cout << "录入matrixA" << endl; input(N, A); cout << "录入matrixB" << endl; input(N, B); cout << "C矩阵" << endl; Strassen(N, A, B, C); output(N, C); cout << "A矩阵" << endl; output(N, A); cout << "B矩阵" << endl; output(N, B); return 0; }
|