标签:
初始化矩阵
void initial_square_matrix(int * * * pm, int n) { pm[0] = new int *[n]; for (int i = 0; i < n; i++) { pm[0][i] = new int[n]; for (int j = 0; j < n; j++) pm[0][i][j] = i * n + j + 1; } }
销毁矩阵
void destroy_square_matrix(int * * * pm, int n) { for (int i = 0; i < n; i++) if (NULL != pm[0][i]) delete[] pm[0][i]; delete[] pm[0]; pm[0] = NULL; }
打印矩阵
void print_square_matrix(int * * m, int n) { cout << "------------------------------------" << endl; for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; j++) cout << m[i][j] << "\t"; cout << endl; } }
向左旋转打印
void print_square_matrix_reversel90(int * * m, int n) { cout << "------------------------------------" << endl; for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; j++) cout << m[j][n - i - 1] << "\t"; cout << endl; } }
向右旋转打印
void print_square_matrix_reverser90(int * * m, int n) { cout << "------------------------------------" << endl; for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; j++) cout << m[n - j - 1][i] << "\t"; cout << endl; } }
很特殊的一种旋转
/* 仅仅适用于类似这样的矩阵 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 */
void reverse_square_matrix(int * * m, int n) { for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; j++) m[i][j] = m[i][j] * (n * n - n + 1) % (n * n + 1); } }
测试
1 int mn = 5; 2 int ** m; 3 4 initial_square_matrix(&m, mn); 5 print_square_matrix(m, mn); 6 print_square_matrix_reversel90(m, mn); 7 print_square_matrix_reverser90(m, mn); 8 reverse_square_matrix(m, mn); 9 print_square_matrix(m, mn); 10 destroy_square_matrix(&m, mn);
标签:
原文地址:http://www.cnblogs.com/datakv/p/5606129.html