CCI_Q1.6

本文参考该作者文章当作编程笔记:

作者:Hawstein
出处:http://hawstein.com/posts/ctci-solutions-contents.html

一.

Q:  一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度。 你能原地进行操作吗?(即不开辟额外的存储空间)

思路:见:http://hawstein.com/posts/1.6.html

CODE:

 1 #include<stdio.h>
 2 #define N 4
 3 #define exch(A,B) {int t=A;A=B;B=t;}
 4 void transpose(int s[][N])
 5 {
 6     int i,j;
 7     for(i=0;i<N;i++)
 8         for(j=i+1;j<N;j++)
 9             exch(s[i][j],s[j][i]);
10     for(i=0;i<N/2;i++)
11         for(j=0;j<N;j++)
12             exch(s[i][j],s[N-1-i][j]);
13 }
14 int main()
15 {
16     int s[N][N]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
17     int i,j;
18     for(i=0;i<N;i++)
19     {
20         for(j=0;j<N;j++)
21             printf("%4d",s[i][j]);
22         printf("
");
23     }
24     printf("转换大法:
");
25     transpose(s);
26     for(i=0;i<N;i++)
27     {
28         for(j=0;j<N;j++)
29             printf("%4d",s[i][j]);
30         printf("
");
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/jhooon/p/3574681.html