码迷,mamicode.com
首页 > 其他好文 > 详细

螺旋数字程序

时间:2014-09-11 02:25:02      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:c 螺旋程序

#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;
}


本文出自 “网络学习总结” 博客,请务必保留此出处http://8947509.blog.51cto.com/8937509/1550925

螺旋数字程序

标签:c 螺旋程序

原文地址:http://8947509.blog.51cto.com/8937509/1550925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!