蛇形输出

int main(int argc, const char * argv[])
{

    int n,m;
    scanf("%d%d",&n,&m);
    int x = 0,y = 0;
    int a = n-1,b = n-1,c = 0,d = 0;
    int st[n][n];
    int ward = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            st[i][j] = 0;        }
    }
    for (int i = 1; i <= m; i++) {
        if (ward == 5) {
            ward = 1;
        }
        if (ward == 1) {
            st[x][y] = i;
//            printf("%d ",st[x][y]);
            if (y == a) {
                ward++;
                c++;
                x = c;
                continue;
            }else{
                y++;
            }
        }
        if (ward == 2) {
            st[x][y] = i;
            if (x == b) {
                ward++;
                a--;
                y = a;
                continue;
            }else{
                x++;
            }
        }
        if (ward == 3) {
            st[x][y] = i;
            if (y == d) {
                ward++;
                b--;
                x = b;
                continue;
            }else{
                y--;
            }
        }
        if (ward == 4) {
            st[x][y] = i;
            if (x == c) {
                ward++;
                d++;
                y = d;
                continue;
            }else{
                x--;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%-3d ",st[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2016-11-15 23:17  小天才努努  阅读(279)  评论(0编辑  收藏  举报