蓝桥杯:打印十字图

问题描述

   小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
提示
请仔细观察样例,尤其要注意句点的数量和输出位置。
 
Thinking:
分析过后,发现这个图形有三个重要的属性:中心,边,角.
从中心开始,首先初始化中心的十字,然后依次从第一条边到第N条边的上下左右四个方向开始填充,
如图:红色部分称为边,蓝色部分称为角,绿色叫中心.
 
#include<iostream>
using namespace std;
void show(char **map,int edgeLen)
{
	for(int i=0;i<edgeLen;i++)
	{
		for(int j=0;j<edgeLen;j++)
			cout<<map[i][j];
		cout<<endl;
	}
}
void fillEdge(char **map,int low,int high,int center,int dis,int edgeLen)
{
	for(int i=low;i<=high;i++)
	{
		map[i][center+dis]=map[i][center-dis]='$';
		map[center+dis][i]=map[center-dis][i]='$';
	}
}
void fillCorner(char **map,int center,int dis)
{
	int topLeft=center-dis,bottomRight=center+dis;
	map[topLeft][topLeft]=map[topLeft][bottomRight]='$';
	map[topLeft-1][topLeft]=map[topLeft][topLeft-1]='$';
	map[bottomRight][topLeft]=map[bottomRight][bottomRight]='$';
	map[topLeft-1][bottomRight]=map[topLeft][bottomRight+1]='$';
	map[bottomRight+1][topLeft]=map[bottomRight][topLeft-1]='$';
	map[bottomRight+1][bottomRight]=map[bottomRight][bottomRight+1]='$';
}
int main()
{
	int i,j,n,center,edgeLen;
	cin>>n;
	edgeLen=4*(n-1)+9;
	center=edgeLen/2;
	char **map=new char*[edgeLen];
	for(i=0;i<edgeLen;i++)
		map[i]=new char[edgeLen];
	for(i=0;i<edgeLen;i++)
		for(j=0;j<=edgeLen;j++)
			map[i][j]='.';
	for(i=center-2;i<=center+2;i++)
		map[i][center]=map[center][i]='$';
	for(i=1;i<=n;i++)
	{
		fillEdge(map,center-i*2,center+i*2,center,2+i*2,edgeLen);
		fillCorner(map,center,i*2);
	}
	show(map,edgeLen);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值