矩阵旋转90度
Posted 指尖起舞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了矩阵旋转90度相关的知识,希望对你有一定的参考价值。
初始化矩阵
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);
以上是关于矩阵旋转90度的主要内容,如果未能解决你的问题,请参考以下文章