#include <stdio.h> int main(int argc, const char * argv[]) { int num,maxnum,count=1,x=0,y,numtemp; int i; printf("输入螺旋列数num,和最大数字maxnum\n"); scanf("%d%d",&num,&maxnum); int index[10][10]={0}; numtemp=num; y=num-1; while (count<=maxnum) { for (i=(int)x; i<num; i++) { if (count<=maxnum) { index[x][i]=count; count++; } else break; }//从左往右 for (i=(int)x+1; i<num; i++) { if (count<=maxnum) { index[i][y]=count; count++; } else break; }//从上往下 for (i=(int)num-2; i>=(int)x; i--) { if (count<=maxnum) { index[num-1][i]=count; count++; } else break; }//从右往左 for (i=(int)num-2; i>=(int)x+1; i--) { if (count<=maxnum) { index[i][x]=count; count++; } else break; }//从下往上 x++; y--; num--; } for (int i=0; i<numtemp; i++) { for (int j=0; j<numtemp; j++) { printf("%d\t",index[i][j]); } printf("\n\n"); } return 0; }
时间: 2024-11-05 02:33:07