历届试题 打印十字图

  历届试题 打印十字图  
时间限制:1.0s   内存限制:256.0MB
      
问题描述

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

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

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

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
提示
请仔细观察样例,尤其要注意句点的数量和输出位置。
 
 
解题思路:这题真的是不需要思路,纯粹就是找规律,我的解题技巧是只找其中四分之一的规律,因为上下左右是相互对称的。
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;

int main(){
    int n;
    cin>>n;
    int m=3+2*n;
    char a[6+4*n][6+4*n];
    for(int i=1;i<=6+4*n;i++){
        memset(a[i],'.',sizeof(a[i]));

    }


     for(int i=1;i<=m;i++){
         for(int j=1;j<=m;j++){
           if(i<=3&&j==1||i==1&&j<=3)a[i][j]='$';
           if(j<=i-2&&i%2)a[i][j]='$';
           if(i<=j-2&&j%2)a[i][j]='$';
           if(j==i-2&&i%2){a[i-1][j]='$';a[i-2][j]='$';}
           if(i==j-2&&j%2)a[i][j-1]='$';


         }
     }
        for(int i=m;i>=1;i--){
         for(int j=m;j>1;j--){
             cout<<a[i][j];
         }
         for(int j=1;j<=m;j++){
             cout<<a[i][j];
         }
         cout<<endl;
       }

      for(int i=2;i<=m;i++){
            for(int j=m;j>1;j--){
            cout<<a[i][j];
        }
        for(int j=1;j<=m;j++){
            cout<<a[i][j];
        }


        cout<<endl;
     }




}
原文地址:https://www.cnblogs.com/xuyibao/p/10519732.html