这里是传送门
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
*CC *CC
C C**
*CC But not *CC
(I use * to show you where are spaces.)
Sample Input
3
0
1
2
Sample Output
C
CC
C
CC
CC CC
C C
CC CC
CC
C
CC
CC CC
C C
CC CC
递归打印图形,从Map[1][1]开始填,把图案的位置填上C,然后判断结束的位置,直接打印一行。
注意数组开大点,然后初始化。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
char Map[3000][3000];
int cnt = 0;
void Print(int n, int x, int y)
{
if(n == 0)
{
Map[x][y] = 'C';
return ;
}
else
{
int len = pow(3, n-1);
Print(n-1, x, y+len);
Print(n-1, x, y+2*len);
Print(n-1, x+len, y);
Print(n-1, x+2*len, y+len);
Print(n-1, x+2*len, y+2*len);
}
}
int main ()
{
int n, t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int len = pow(3, n);
memset(Map,' ', sizeof(Map));
Print(n, 1, 1);
for(int i = 1; i <= len; i++)
{
for(int j = len; j >= 1; j--)
{
if(Map[i][j] == 'C')
{
Map[i][j+1] = '\0';
break;
}
}
}
for(int i = 1; i <= len; i++)
printf("%s\n", Map[i]+1);
}
return 0;
}
另外附一个题目:打印图形(2018蓝桥杯)
类似的问题,感觉比上面的方法更加复杂一点,记录一下。
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
void show(char* buf, int w)
{
int i,j;
for(i=0; i<w; i++)
{
for(j=0; j<w; j++)
{
printf("%c", buf[i*w+j]==0? ' ' : 'o');
}
printf("\n");
}
}
void draw(char* buf, int w, int x, int y, int size)
{
if(size==1)
{
buf[y*w+x] = 1;
return;
}
int n = size/3 ; //МоїХ
draw(buf, w, x, y, n);
draw(buf, w, x-n, y,n);
draw(buf, w, x+n, y,n);
draw(buf, w, x, y-n,n);
draw(buf, w, x, y+n,n);
}
int main()
{
int N;
while(scanf("%d", &N) == 1)
{
int t = pow(3, N);
int i;
char* buf = (char*)malloc(t*t);
memset(buf, 0, sizeof(buf));
draw(buf, t, t/2, t/2, t);
show(buf, t);
free(buf);
}
return 0;
}