Hust oj 2112 Print(递归)

本文介绍了一个使用递归方法实现的算法,该算法可以根据输入的整数打印出特定的字符图案。通过递归调用和字符填充的方式,可以生成不同大小和形状的图形,适用于初学者理解和实践递归思想。

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

Print
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 118(77 users)Total Accepted: 88(75 users)Rating: Special Judge: No
Description
Give you a number, print characters like this:
1:
*

2:
...
.*.
...

3:
*****
*...*
*.*.*
*...*
*****
Input

The input contains multiple test cases. Each case consist of a integer n.

1<=n<=50.


Output

For each test case, print the character table like the examples.

Sample Input
1
2
3
4
5
Sample Output
*
...
.*.
...
*****
*...*
*.*.*
*...*
*****
.......
.*****.
.*...*.
.*.*.*.
.*...*.
.*****.
.......
*********
*.......*
*.*****.*
*.*...*.*
*.*.*.*.*
*.*...*.*
*.*****.*
*.......*
*********
递归模拟
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int N;

const int maxn = 1005;
char a[maxn][maxn];
void Printf(int n,int ans)
{
    if(n == 0) return ;
    else if(n % 2 == 1)
    {
        for(int i=1;i<=2*n-1;i++)
        {
            a[ans+1][i+ans] = a[2*N-1-ans][i+ans] = '*';
            a[i+ans][ans+1] = a[i+ans][2*N-1-ans] = '*';
        }
    }
    else
    {
        for(int i=1;i<=2*n-1;i++)
        {
            a[ans+1][i+ans] = a[2*N-1-ans][i+ans] = '.';
            a[i+ans][ans+1] = a[i+ans][2*N-1-ans] = '.';
        }
    }
    Printf(n-1,ans+1);
}
int main()
{
    while(~scanf("%d",&N))
    {
       Printf(N,0);
        for(int i=1;i<=2*N-1;i++)
        {
            for(int j=1;j<=2*N-1;j++)
            {
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值