蓝桥杯:打印十字图

来源:互联网 发布:c语言数学函数库 编辑:程序博客网 时间:2024/04/29 07:22
问题描述

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

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

对方同时也需要在电脑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;}

0 0
原创粉丝点击