- 论坛徽章:
- 0
|
也来发个第一题的解答
- #include<stdio.h>
- int main(void)
- {
- int row=3,col=5;
- int a[row][col];
- int n=0;
- int i,j;
- enum direction {right,down,left,up};
- enum direction f = right;
- for(i=0;i<row;i++)
- for(j=0;j<col;j++)
- a[i][j]=0;
- i=j=0;
- int total = row*col;
- while(1)
- {
- if(n >= total )
- break;
- a[i][j]=++n;
- switch(f)
- {
- case right:
- if(j+1 >col-1 || a[i][j+1] > 0)
- {
- f++;i++;
- }
- else
- j++;
- break;
- case down:
- if(i+1>row-1 || a[i+1][j] >0)
- {
- f++;j--;
- }
- else
- i++;
- break;
- case left:
- if(j-1<0 || a[i][j-1] > 0)
- {
- f++;i--;
- }
- else
- j--;
- break;
- case up:
- if(i-1< 0|| a[i-1][j] > 0)
- {
- f=right;j++;
- }
- else
- i--;
- break;
- }
- }
- for(i=0;i<row;i++)
- {
- for(j=0;j<col;j++)
- printf("%-d\t",a[i][j]);
- printf("\n");
- }
- return 0;
- }
复制代码 |
|