圆圈(图形输出问题,递归)

链接:https://www.nowcoder.com/acm/contest/67/G
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
圈圈圆圆圈圈,lulu小朋友最近看喜羊羊看多了,老是受刺激就画圆圈,听到小于8的数字时,还会画出十分有规律的圆圈,现在你需要根据样例观察出规律,编写程序,根据输入的数字n(n<8),输出对应的圆圈。

输入描述:
第一行是样例数T(T<9)
第2到2+T-1行每行有一个整数n(n<8),代表lulu听到的数字
输出描述:
听到对应数字时,输出对应样子的圆圈。
示例1
输入
4
0
1
2
3
输出
O
O
O O
O
O
O O
O
O O
O O O O
O O
O
O O
O
O
O O
O
O O
O O O O
O O
O
O O
O
O O
O O O O
O O
O O O O
O O O O O O O O
O O O O
O O
O O O O
O O
O
O O
O
O O
O O O O
O O
O
O O
O
说明
当n=0时输出
O
当n=1时输出
*O
O*O
*O
当n=2时输出
****O
***O*O
****O
*O*****O
O*O***O*O
*O*****O
****O
***O*O
****O
上面的’O’是大写英文字母O,’*’代表空格,每一行最后一个O后面不带空格。
备注:
对于100%的数据,

0<T<9;
0<=n<8;

分析: 补题 图形输出问题,poj 1491就是一个例子。http://blog.csdn.net/feng_zhiyu/article/details/79172844

本题中解题步骤:
1.找到中心点((h+1)/2,(h+1)/2);
2. 递归输出上下左右的图形
3. 输出时控制 格式

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define rep(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int INF=0x3f3f3f3f;
const double PI=4*atan(1.0);
const int MOD=1e9+7;
const double eps=1e-8;
const int N=2200+5;
char a[N][N];
inline ll Pow(ll a,ll n)
{
    ll ans=1;
    rep(i,0,n) ans*=a;
    return ans;
}
void draw(int x,int y,int n)///(x,y)为当前的点,n为当前圆圈层数
{
    if(!n)
    {
        a[x][y]='O';
        return;
    }
    int d=Pow(3,n-1);
    draw(x-d,y,n-1);///上面的图形
    draw(x+d,y,n-1);///下面
    draw(x,y-d,n-1);///左边
    draw(x,y+d,n-1);///右边
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        mem(a,' ');
        scanf("%d",&n);
        int h=Pow(3,n);
        int x=(h+1)>>1;
        draw(x,x,n);
        rep(i,1,h+1)
        {
            for(int j=h; j>=1; j--)
                if(a[i][j]=='O')
                {
                    a[i][j+1]='\0';
                    break;
                }
        }
        rep(i,1,h+1) puts(a[i]+1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值