Alibaba 矩阵逆置

尽量减少空间复杂度。

View Code
 1 //矩阵旋转,顺时针90,180,270度
2
3 #include<iostream>
4 using namespace std;
5
6 #define M 4
7 #define N 3
8
9 char source[M][N];
10 char rotate1[N][M];
11 char rotate2[M][N];
12 char rotate3[N][M];
13
14
15
16
17 void revolve1()
18 {
19 char *p=source[M-1]; //记住一点,b和&b[0]意思一样,都是行地址。
20 for(int i=0;i<N;i++)
21 {
22 for(int j=0;j<M;j++)
23 {
24 rotate1[i][j]=*(p-N*j+i);
25 cout<<rotate1[i][j]<<"";
26 }
27 cout<<endl;
28 }
29 cout<<endl;
30 }
31
32 void revolve2()
33 {
34 char *p=&source[M-1][N-1];
35 for(int i=0;i<M;i++)
36 {
37 for(int j=0;j<N;j++)
38 {
39 rotate2[i][j]=*(p-N*i-j);
40 cout<<rotate2[i][j]<<"";
41 }
42 cout<<endl;
43 }
44 cout<<endl;
45 }
46
47 void revolve3()
48 {
49 char *p=source[0];
50 for(int i=0;i<N;i++)
51 {
52 for(int j=0;j<M;j++)
53 {
54 rotate3[i][j]=*(p+N*j+N-1-i); //*p=&source[0][N-1]; rotate3[i][j]=*(p-i+N*j); 注意是该行右边忘记减1,加N就加了一整行了。
55 cout<<rotate3[i][j]<<"";
56 }
57 cout<<endl;
58 }
59 cout<<endl;
60 }
61
62
63 void test()
64 {
65 for(int i=0;i<M;i++)
66 for(int j=0;j<N;j++)
67 cin>>source[i][j];
68 for(int i=0;i<M;i++)
69 {
70 for(int j=0;j<N;j++)
71 cout<<source[i][j]<<"";
72 cout<<endl;
73 }
74 cout<<endl;
75 revolve1();
76 revolve2();
77 revolve3();
78 }
79
80 int main()
81 {
82 test();
83 return 0;
84 }
原文地址:https://www.cnblogs.com/YipWingTim/p/2240558.html