Description
interesting party. Almost everyone in the world wants to take part in his party. Now, he needs to draw some beautiful invitations for the people who will take part in his party. PY is so busy that he gives this mission to you. Plenty of girls wait to receive PY’s invitations, so you’d better to draw them quickly.
You should only use ‘o’ or ‘ ’ to print the triangle just like this:
o
o o
And just as the recursion depth increased, the triangle that you need to output will be bigger and bigger. More details please to see the sample output.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. The only line of each test case contains an integer N (1 <= N <= 10) indicates the recursion depth.
Output
For each test case draw an outline of the triangle. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.
Sample Input
3
1
2
3
Sample Output
案例:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
char a[3005][3005];
void dfs(int cur,int x,int y)
{
a[x][y]='o';
if(cur==1)return;
int k=pow(2,cur-1);
dfs(cur-1,x,y);
dfs(cur-1,x+k,y-k);
dfs(cur-1,x+k,y+k);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(a,' ',sizeof(a));
int s=pow(2,n);
dfs(n,1,s);
dfs(n,2,s-1);
dfs(n,2,s+1);
for(int i=1; i<=s; i++)
{
for(int j=pow(2,n+1); j>=1; j--)
if(a[i][j]=='o')
{
a[i][j+1]='\0';
break;
}
}
for(int i=1; i<=s; i++)
{
printf("%s\n",a[i]+1);
}
printf("\n");
}
}
思路:
第二次做递归画图的题,感觉还是好难。
第一次做的:
http://blog.youkuaiyun.com/li_hongcheng/article/details/79337959
这种递归画图,找规律是难点,然后想递归过程。