题目链接
题目大意
要求:给定一个正整数 nn,参考输出样例,输出图形。
输入与输出样例
输入1
2
输出1
/\
/__\
/\ /\
/__\/__\
输入2
3
输出2
/\
/__\
/\ /\
/__\/__\
/\ /\
/__\ /__\
/\ /\ /\ /\
/__\/__\/__\/__\
思路
当n=1的时候
/\
/__\
不难发现,图腾 n n n的形状是基于图腾 n − 1 n-1 n−1分别从上、左下、右下拼接成的。接下通过分治递归完成该图的拼接即可。
AC代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn = 2e3 + 5;
char mp[maxn][2 * maxn];//储存图腾
void DFS(int x, int y, int n)
{
//画图腾
if (n == 1)
{
mp[x][y] = '/';
mp[x][y + 1] = '\\';
mp[x + 1][y - 1] = '/';
mp[x + 1][y + 2] = '\\';
mp[x + 1][y] = '_';
mp[x + 1][y + 1] = '_';
return;
}
int s = 2 << (n - 2);
//上
DFS(x, y, n - 1);
//左下
DFS(x + s, y - s, n - 1);
//右下
DFS(x + s, y + s, n - 1);
}
int main(int argc, char* argv[])
{
int n;
scanf("%d", &n);
memset(mp, ' ', sizeof(mp));
DFS(1, pow(2, n), n);
for (int i = 1; i <= 2 << (n-1); ++i)
{
for (int j = 1; j <= 2 << n ; ++j)
printf("%c", mp[i][j]);
printf("\n");
}
return 0;
}