蓝桥杯 打印十字图

来源:互联网 发布:淘宝怎么搜索高仿手表 编辑:程序博客网 时间:2024/04/27 20:38
问题描述

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

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

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

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..

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

思路:解决此类题目最重要的就是要发现规律,我们发现只要求出该图形的1/4就可以了,其它的地方都是对称的

所以AC代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;char str[150][150];int main(){    int n;    //freopen("in.cpp","r",stdin);    //freopen("out.cpp","w",stdout);    while(cin>>n)    {        str[1][1]=str[2][1]=str[2][2]='.';        int mid=(4*n+5)/2+1;        for(int i=3; i<=mid; i++)        {            if((i-3)%2==0)            {                int j;                for( j=1; j<=i-3; j++)                {                    if(j%2==0)                        str[i][j]='.';                    else                        str[i][j]='$';                   // cout<<str[i][j];                }                for(; j<=i; j++)                {                    str[i][j]='$';                   // cout<<str[i][j];                }                //cout<<endl;            }            else            {                int j;                for( j=1; j<=i-2; j++)                {                    if(j%2==0)                        str[i][j]='.';                    else                        str[i][j]='$';                   // cout<<str[i][j];                }                for(; j<=i; j++)                {                    str[i][j]='.';                  //  cout<<str[i][j];                }              //  cout<<endl;            }        }        for(int i=1; i<=mid; i++)        {            for(int j=i+1; j<=mid; j++)            {                str[i][j]=str[j][i];              //  cout<<str[i][j];            }           // cout<<endl;        }      /*  for(int i=1;i<=mid;i++)        {            for(int j=1;j<=mid;j++)            {                cout<<str[i][j];            }            cout<<endl;        }*/        for(int i=1; i<=mid; i++)        {            for(int j=mid+1; j<=(4*n+5); j++)            {                str[i][j]=str[i][4*n+5-j+1];                //cout<<4*n+5-j+1<<" ";                //cout<<str[i][j];            }           // cout<<endl;        }        for(int i=1; i<=mid ; i++)        {            for(int j=1; j<=(4*n+5); j++)                cout<<str[i][j];            cout<<endl;        }        for(int i=mid-1; i>=1; i--)        {            for(int j=1; j<=(4*n+5); j++)                cout<<str[i][j];            cout<<endl;        }    }    return  0;}


1 0
原创粉丝点击