分形问题 递归打印解决 另附hrbust 2291 题目

本博客为博主处女贴    如有问题欢迎指出

--------------------------------------------------------------------------------

虽然不太可能 但是如有转载 请注明出处



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

*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

 

 

本类型题其实就是模版体  比赛的时候有模版还是很好做的  (其实没有模版也不难)

 

最主要的是格式  对输出图形空格的格式   以及没两组输出后又没有空格等

 

对不同的图行 只要找到相应的位置不断的递归就可以了 

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

 

附本题代码

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值