码迷,mamicode.com
首页 > 编程语言 > 详细

HLG 1564 螺旋矩阵 (趣味C语言)

时间:2014-06-09 23:23:32      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1564


Description
对于给定的一个数n,要你打印n*n的螺旋矩阵。

比如n=3时,输出:

1 2 3
8 9 4
7 6 5

Input

多组测试数据,每个测试数据包含一个整数n(1<=n<=32)

Output

对于每组测试数据,输出一个n*n的螺旋矩阵,定义在题目描述里。

在一组测试数据中,每个数占的字符宽度是该组数据中最大的数位数加1,比如3*3的螺旋矩阵,最大值是9,那么每个数就要占2个字符宽度。

两组测试数据之间用一个空行隔开。

Sample Input
1
2
3
Sample Output
1

1 2
4 3

1 2 3
8 9 4
7 6 5


代码如下:(怀念初学的时候啊~~~)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 105
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

int Kesha[105][105], cas = 0, n;

int main()
{
    while(~scanf("%d", &n)) {
        if(cas++) printf("\n");
        int x, y, ans = 0;
        RST(Kesha);
        ans = Kesha[x=0][y=0] = 1;

        while(ans < n * n) {
            while(y + 1 < n && !Kesha[x][y+1]) Kesha[x][++y] = ++ans;
            while(x + 1 < n && !Kesha[x+1][y]) Kesha[++x][y] = ++ans;
            while(y - 1 >= 0 && !Kesha[x][y-1]) Kesha[x][--y] = ++ans;
            while(x - 1 >= 0 && !Kesha[x-1][y]) Kesha[--x][y] = ++ans;
        }
        int t = n * n;
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(t<10) printf("%2d", Kesha[i][j]);
                else if(t>=10 && t<=99) printf("%3d", Kesha[i][j]);
                else if(t>=100 && t<=999) printf("%4d", Kesha[i][j]);
                else if(t >= 1000 ) printf("%5d", Kesha[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}


HLG 1564 螺旋矩阵 (趣味C语言),布布扣,bubuko.com

HLG 1564 螺旋矩阵 (趣味C语言)

标签:des   c   style   class   blog   code   

原文地址:http://blog.csdn.net/keshacookie/article/details/28907487

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