HRBUST - 2291 Help C5

本文介绍了一种使用递归算法打印复杂图形的方法,包括签名图案和蓝桥杯比赛中的图形。通过逐步分解图形,利用递归函数填充字符,实现图形的精确绘制。文章提供了C语言代码示例,展示了如何初始化和打印不同层级的图形。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里是传送门

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值