2018年全国多校算法寒假训练营练习比赛(第一场)G. 圆圈

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

题目描述

    圈圈圆圆圈圈,lulu小朋友最近看喜羊羊看多了,老是受刺激就画圆圈,听到小于8的数字时,还会画出十分有规律的圆圈,现在你需要根据样例观察出规律,编写程序,根据输入的数字n(n<8),输出对应的圆圈。

输入描述:

第一行是样例数T(T<9)
第2到2+T-1行每行有一个整数n(n<8),代表lulu听到的数字

输出描述:

听到对应数字时,输出对应样子的圆圈。


一种比较无脑的方法就是递归处理,其中n=7图形就相当于是

空格   n=6

n=6   空格   n=6

空格   n=6

注意细节和换行

#include<stdio.h>
#include<string.h>
int san[8] = {0,1,3,9,27};
char ans[2500][2500];
void Print(int x, int a, int b, int type)
{
	int i, j;
	if(x==0)
	{
		ans[a][b] = 'O';
		return;
	}
	for(i=a;i<a+san[x];i++)
	{
		for(j=b;j<b+san[x];j++)
			ans[i][j] = ' ';
	}
	Print(x-1, a, b+san[x], 0 || type);
	Print(x-1, a+san[x], b, 1);
	for(i=a+san[x];i<a+san[x]*2;i++)
	{
		for(j=b+san[x];j<b+san[x]*2;j++)
			ans[i][j] = ' ';
	}
	Print(x-1, a+san[x], b+san[x]*2, 0 || type);
	for(i=a+san[x]*2;i<a+san[x]*3;i++)
	{
		for(j=b;j<b+san[x];j++)
			ans[i][j] = ' ';
	}
	Print(x-1, a+san[x]*2, b+san[x], 0 || type);
	if(type==1)
	{
		for(i=a;i<a+san[x];i++)
		{
			for(j=b+san[x]*2;j<b+san[x]*3;j++)
				ans[i][j] = ' ';
		}
		for(i=a+san[x]*2;i<a+san[x]*3;i++)
		{
			for(j=b+san[x]*2;j<b+san[x]*3;j++)
				ans[i][j] = ' ';
		}
	}
}
int main(void)
{
	int T, n, i, j;
	for(i=2;i<=7;i++)
		san[i] = san[i-1]*3;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		memset(ans, 0, sizeof(ans));
		if(n==0)
			printf("O\n");
		else
		{
			Print(n, 1, 1, 0);
			for(i=1;i<=san[n]*3;i++)
			{
				for(j=1;j<=san[n]*3;j++)
				{
					if(ans[i][j]==0)
						break;
					printf("%c", ans[i][j]);
				}
				puts("");
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值