首页 > 其他 > 详细

蓝桥杯 打印十字图

时间:2015-01-21 18:18:24      阅读:283      评论:0      收藏:0      [点我收藏+]
问题描述

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

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

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


蓝桥杯 打印十字图

原文:http://blog.csdn.net/u012313382/article/details/42970625

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!