[c语言练习]打印数字螺旋矩形

下图是一个数字螺旋数组:

1       56      55       54       53       52       51       50      49       48        47       46       45      44      43
2       57      104     103     102     101     100     99      98       97        96       95       94      93      42
3       58      105     144     143     142     141     140     139     138     137     136     135     92      41
4       59      106     145     176     175     174     173     172     171     170     169     134     91      40
5       60      107     146     177     200     199     198     197     196     195     168     133     90      39
6       61      108     147     178     201     216     215     214     213     194     167     132     89      38
7       62      109     148     179     202     217     224     223     212     193     166     131     88      37
8       63      110     149     180     203     218     225     222     211     192     165     130     87      36
9       64      111     150     181     204     219     220     221     210     191     164     129     86      35
10      65      112     151     182     205     206     207     208     209     190     163     128     85      34
11      66      113     152     183     184     185     186     187     188     189     162     127     84      33
12      67      114     153     154     155     156     157     158     159     160     161     126     83      32
13      68      115     116     117     118     119     120     121     122     123     124     125     82      31
14      69      70       71       72       73       74        75       76      77        78      79        80      81      30
15      16      17       18       19       20       21         22       23      24        25      26       27       28      29
题目的要求就是编写一个程序打印N*N的数字螺旋数组.比如,上图是一个15*15的数字螺旋数组.

代码由c语言写成,使用了递归思路,方法较笨拙,但实现了基本功能.

#include <stdio.h>

#include <stdlib.h>



#define MAX 15

int result[MAX][MAX]={0};

void  rotate(int step,int x,int y);



int num;



int main(){



        char dechar;

        int i,j;

        result[0][0]=1;



ain:

        printf("enter a  number(1-%d):",MAX);

        scanf("%d",&num);

        getchar();



        if(num<=0 || num > MAX){

                printf("enter wrong nuber,please enter a number between(1-%d)\n",MAX);

                goto ain;

                exit(1);

        }



        for(i=0;i<MAX;i++)

                for(j=0;j<MAX;j++)

                if(0==i&&0==j)

                        continue;

                else

                        result[i][j]=0;

        rotate(num,0,0);

        printf("\n");

        printf("want again?(Y/y is execute again)\t");

        scanf("%c",&dechar);

        getchar();



        if(dechar=='Y'||dechar=='y')

                goto ain;

        else

                return 0;



}





void rotate(int step,int x,int y){



        int i,j,k_step;

        int r,s;

        if(1==step)

        {

                result[x][y]=num*num;

                for(i=0;i<MAX;i++)

                        for(j=0;j<MAX;j++){

                        printf("%d\t",result[i][j]);

                        if((MAX-1)==j) printf("\n");

                        }

        }

#if 1

        else if(0==step){

                for(i=0;i<MAX;i++)

                        for(j=0;j<MAX;j++){

                        printf("%d\t",result[i][j]);

                        if((MAX-1)==j) printf("\n");

                        }

        }

#endif

        else{

                k_step=step;

                for(i=y+1;k_step>1;k_step--,i++)

                        result[i][y]=result[x][y]+i-y;

                i=i-1;



                k_step=step;

                for(j=x+1;k_step>1;k_step--,j++)

                        result[i][j]=result[i][x]+j-x;

                j=j-1;



                k_step=step;

                for(r=j-1;k_step>2;k_step--,r--)

                        result[s][r]=result[s][j]+j-r;



                result[x+1][y+1]=result[x][y+1]+1;



                rotate(step-2,x+1,y+1);

        }

}

 

 

你可能感兴趣的:(C语言)