哈理工OJ 2291 Help C5(分形题目)

本文介绍了一个特殊的编程挑战,即设计一个能够自动绘制一系列特定字符签名的程序。该程序使用递归方式构建签名,并通过调整参数来适应不同字符的需求。文章包含了一个完整的C++实现示例,展示了如何通过算法构建复杂的图案。

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2291
Help C5
Time Limit: 1000 MS Memory Limit: 65535 K
Total Submit: 170(39 users) Total Accepted: 50(37 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


上面的图形和原题中不符,建议去原题中看图形
------------------------------------------
 -------------------------------------

下面是AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char a[3000][3000];

int mypow(int n)
{
    int ans=1;
    for(int i=1; i<=n; i++)
    {
        ans=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,y+s);
    dfs(cur-1,x,y+2*s);
    dfs(cur-1,x+s,y);
    dfs(cur-1,x+2*s,y+s);
    dfs(cur-1,x+2*s,y+2*s);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        memset(a,' ',sizeof(a));
        scanf("%d",&n);
        dfs(n+1,1,1);
        int s=mypow(n);
        for(int i=1; i<=s; i++)
        {
            for(int j=s;j>=1;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);//从每行的第二个开始输出
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值