校赛 A题 (递归)

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

n == 1 时:

+------+
|      |
| /  \ |
|      |
| +--+ |
| |  | |
| |  | |
+-+--+-+
n == 2 时:

+--------------+
|              |
|     /  \     |
|    /    \    |
|   /      \   |
|  /        \  |
| /          \ |
|              |
|   +------+   |
|   |      |   |
|   | /  \ |   |
|   |      |   |
|   | +--+ |   |
|   | |  | |   |
|   | |  | |   |
+---+-+--+-+---+
有T组测试数据,每组数据包含一个n,输出对应的图形;


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 1000
using namespace std;

void draw(const int n, char map[][MAXN], const int r, const int c)
{
    int size = (1 << (n+2));
    map[r][c] = map[r+size-1][c+size-1] = map[r][c+size-1] = map[r+size-1][c] = '+';
    for(int i=1; i<size-1; i++) {
        map[r][c+i] = map[r+size-1][c+i] = '-';
        map[r+i][c] = map[r+i][c+size-1] = '|';
    }
    if(n == 0) return ;
    for(int i=2; i<size/2-1; i++) {
        map[r+i][c+size/2-i] = '/';
        map[r+i][c+size/2+i-1] = '\\';
    }
    draw(n-1, map, r+size/2, c+size/4);
}

int main()
{
    int cas, n;
    scanf("%d", &cas);
    while(cas--) {
        static char map[MAXN][MAXN];
        scanf("%d", &n);
        memset(map, ' ', sizeof(map));
        draw(n, map, 0, 0);
        int size = (1 << (n+2));
        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) putchar(map[i][j]);
            putchar(10);
        }
    }
    return 0;
}


你可能感兴趣的:(校赛 A题 (递归))