zoukankan      html  css  js  c++  java
  • 试题 历届试题 打印十字图

    资源限制
    时间限制:1.0s   内存限制:256.0MB
     
    问题描述

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

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

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

    输入格式
    一个正整数 n (n<30) 表示要求打印图形的层数。
     
    输出格式
    对应包围层数的该标志。
     
    样例输入1
    1
     
    样例输出1
    ..$$$$$..
    ..$...$..
    $$$.$.$$$
    $...$...$
    $.$$$$$.$
    $...$...$
    $$$.$.$$$
    ..$...$..
    ..$$$$$.. 
     
    样例输入2
    3
     
    样例输出2
    ..$$$$$$$$$$$$$..
    ..$...........$..
    $$$.$$$$$$$$$.$$$
    $...$.......$...$
    $.$$$.$$$$$.$$$.$
    $.$...$...$...$.$
    $.$.$$$.$.$$$.$.$
    $.$.$...$...$.$.$
    $.$.$.$$$$$.$.$.$
    $.$.$...$...$.$.$
    $.$.$$$.$.$$$.$.$
    $.$...$...$...$.$
    $.$$$.$$$$$.$$$.$
    $...$.......$...$
    $$$.$$$$$$$$$.$$$
    ..$...........$..
    ..$$$$$$$$$$$$$.. 
     
    提示
    请仔细观察样例,尤其要注意句点的数量和输出位置。
     
     
     
    为方便观察,于是把题中的图案用填充了颜色的表格表现出来,白色区域代表'.',蓝色区域代表'$',以下分别是n=1和n=3时对应的图案——
     
                        
     
    从图中可以知道,打印的图形是中心对称的,因此只需分析左上角那块,通过对称就能把其余部分补上了.
     
    填充可以分成三个部分:中心的十字、对角线、上下左右的边,如下图——
     
     
    可以发现,每加一层,图形的长/宽len就会加4,从而得出len=n*4+5; 因为n<30,所以数组开到130足矣.
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <string>
     6 #include <cmath>
     7 #include <algorithm>
     8 #define INF 0x3f3f3f3f
     9 #define zero 1e-7
    10 
    11 using namespace std;
    12 typedef long long ll;
    13 const ll mod=50000;
    14 const ll max_n=2e5+7;
    15 
    16 char g[130][130]; 
    17 int n;
    18 int len;//图形的长/宽 
    19 
    20 void init() {
    21     for(int i=0; i<len; i++) 
    22         for(int j=0; j<len; j++) 
    23             g[i][j]='.';
    24 }
    25 
    26 void fil() {
    27     int cen=len/2;//中心点
    28     //填充中间的十字 
    29     g[cen][cen]='$';
    30     g[cen][cen-2]=g[cen][cen-1]=g[cen][cen+1]=g[cen][cen+2]='$';
    31     g[cen-2][cen]=g[cen-1][cen]=g[cen+1][cen]=g[cen+2][cen]='$';
    32     //填充对角线位置
    33     for(int i=2; i<cen; i+=2) {
    34         g[i][i]=g[i][i-1]=g[i-1][i]='$';//左上 
    35         g[i][len-i-1]=g[i][len-i]=g[i-1][len-i-1]='$';//右上 
    36         g[len-i-1][i]=g[len-i-1][i-1]=g[len-i][i]='$';//左下 
    37         g[len-i-1][len-i-1]=g[len-i-1][len-i]=g[len-i][len-i-1]='$';//右下 
    38     } 
    39     //填充上下左右位置
    40     for(int i=0; i<=cen-2; i+=2) {
    41         for(int j=i+2; j<=cen; j++) {
    42             g[i][j]=g[i][len-j-1]='$';//
    43             g[len-i-1][j]=g[len-i-1][len-j-1]='$';//
    44             g[j][i]=g[len-j-1][i]='$';//
    45             g[j][len-i-1]=g[len-j-1][len-i-1]='$';//
    46         }
    47     } 
    48 }
    49 
    50 void print() {
    51     for(int i=0; i<len; i++) {
    52         for(int j=0; j<len; j++) {
    53             printf("%c", g[i][j]);
    54         }
    55         printf("
    ");
    56     }
    57 } 
    58 
    59 int main() {
    60     cin>>n;
    61     len=n*4+5;
    62     init();
    63     fil();
    64     print();
    65     return 0;
    66 } 
     
  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/wwqzbl/p/13554981.html
Copyright © 2011-2022 走看看