蛇形数组

输入N阶矩阵1,2.......n*n,逆时针打印数组.

例如:N=4

1    2     3    4

5    6     7    8  

9    10  11  12    

13  14  15  16

经过变化后:

1     2    3    4

12  13   14   5  

11  16   15   6   

10   9     8   7

具体实现过程参考代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<malloc.h>
 4 #define max 100
 5 int main(void)
 6 {
 7     int n;
 8     int count=1;
 9     int x,y,round;
10     scanf("%d",&n);
11     int a[max][max];
12     if(n==1)
13     {
14         a[0][0]=count;
15     }
16     else
17         for(round=0;round<n/2;round++)
18     {
19             x=round;
20         for(y=round;y<n-round;y++)
21         {    a[x][y]=count;
22              count++;
23         }
24         y=n-round-1;
25         for(x=round+1;x<n-round-1;x++)
26         {
27             a[x][y]=count;
28             count++;
29         }
30         x=n-round-1;
31         for(y=n-round-1;y>=round;y--)
32         {
33             a[x][y]=count;
34             count++;
35         }
36         y=round;
37         for(x=n-round-1-1;x>round;x--)
38         {
39             a[x][y]=count;
40             count++;
41         }
42         }
43         if(n%2==1){a[n/2][n/2]=count;}
44         for(x=0;x<n;x++)
45         {
46             for(y=0;y<n;y++)
47             {
48                 printf("%d ",a[x][y]);
49             }
50         }
51             printf("\n");
52             return 0;
53         }
蛇形数组

 

posted on 2016-05-01 10:33  wxdjss  阅读(431)  评论(0编辑  收藏  举报

导航