#include <iostream> #include <iomanip> using namespace std; #define SIZE 8 int main(int argc, char **argv[]) { int matrix[SIZE][SIZE] = {0}; int a[SIZE][SIZE] = { 0 }; int *p = nullptr; p = &matrix[0][0]; //初始化矩阵 for (int k = 0; k < SIZE*SIZE; k++) { *p++ = k; } //打印原始矩阵 cout << "原始矩阵如下:" << endl; for (int k= 0;k < SIZE;k++) { for (int h = 0; h < SIZE;h++) { cout << setw(4) << *(*(matrix + k) + h); } cout << endl; } //Z字形编排 int i = 0, j = 0; //变量不能重复 for (int x = 0; x < SIZE;x++) { for (int y = 0; y < SIZE;y++) { *(*(a + i) + j) = *(*(matrix + x) + y); if((i==SIZE-1||i==0)&&j%2==0) //水平右移 { j++; continue; } if ((j==0||j==SIZE-1)&&i%2==1) //垂直下移 { i++; continue; } if ((i+j)%2==0) //右上 { i--; j++; } else if ((i+j)%2==1) //左下 { i++; j--; } } } cout << endl << "经过Z字形编排后的矩阵如下:" << endl; for (int i = 0; i < SIZE;i++) { for (int j = 0; j < SIZE;j++) { cout << setw(4) << *(*(a + i) + j); } cout << endl; } return 0; }
运行结果:
原始矩阵如下: 0 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 经过Z字形编排后的矩阵如下: 0 1 5 6 14 15 27 28 2 4 7 13 16 26 29 42 3 8 12 17 25 30 41 43 9 11 18 24 31 40 44 53 10 19 23 32 39 45 52 54 20 22 33 38 46 51 55 60 21 34 37 47 50 56 59 61 35 36 48 49 57 58 62 63 请按任意键继续. . .
时间: 2024-12-26 01:43:50