[原创]分形问题 递归打印解决 另附hrbust 2291 题目

2015-12-12 13:06:01 Tabris_ 阅读数:990


博客爬取于2020-06-14 22:45:19
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/50274193


Help C5

Time Limit: 1000 MS

Memory Limit: 65535 K

Total Submit: 51(9 users)

Total Accepted: 12(8 users)

Rating:

Special Judge: No

Description

Hello, I’m Sea5, and you can call me C5 instead. I want a program which can sign my name automatically. And my brothers, C0, C1, C2, C3, C4, C6, C7, C8, each of them wants one as well. Can you help us?

Input

First line is the number of test cases T(T<=8).

T lines follow, each line includes an integer N(N<=7), and you should help C(N) to sign his name.

Output

C0’s signature is ‘C’.

When you draw C(N)’s name, you should print the name using C(N-1)’s name as its element, and using the following format to draw it.

*XX

X**

*XX

(X is the element, * is blank space)

And please don’t print extra spaces at the end of line.

For example, C1’s name should be

1
2
3
4
5
*CC                    *CC

C C**

*CC But not *CC

(I use * to show you where are spaces.)

Sample Input

1
2
3
4
3
0
1
2

Sample Output

1
2
3
4
5
6
7
8
9
10
11
12
13
C
CC
C
CC
CC CC
C C
CC CC
CC
C
CC
CC CC
C C
CC CC

本类型题其实就是模版体 比赛的时候有模版还是很好做的 (其实没有模版也不难)
最主要的是格式 对输出图形空格的格式 以及没两组输出后又没有空格等
对不同的图行 只要找到相应的位置不断的递归就可以了

其实也可以先把所有的图形都打印下来 然后在选取部分输出就好了

附本题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <cstdio> //分形问题递归打印解决
#include <cstring>
#include <iostream>
using namespace std;

char a[3000][3000]; //注意数组的大小 不然存不下图形 会RE

int mypow(int d)
{
int ans = 1;
for (int i = 1; i <= d; i++)
ans *= 3;
return ans;
}

void dfs(int cur, int x, int y)
{
if (cur == 1)
{
a[x][y] = 'C';
return;
}

int s = mypow(cur - 2);
dfs(cur - 1, x + s, y); //这些按照格式来就好
dfs(cur - 1, x, y + 2 * s);
dfs(cur - 1, x, y + s);
dfs(cur - 1, x + 2 * s, y + s);
dfs(cur - 1, x + 2 * s, y + 2 * s);
}

int main(void)
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
n++;
memset(a, ' ', sizeof(a));
dfs(n, 1, 1);

int s = mypow(n - 1);
for (int i = 0; i <= s; i++) //先把最大的边界找到
a[i][s + 1] = '\0';

for (int i = 0; i <= s; i++) //其实格式可以每行从后往前来把每个编程\0遇到不是空格就停止
{
for (int j = s + 1; j >= 0; j--)
{
if (a[i][j] == 'C')
{
a[i][j + 1] = '\0';
break;
}
}
}

for (int i = 1; i <= s; i++)
printf("%s\n", a[i] + 1); //a[i]+1 就是从a[1]开始输出
}
return 0;
}