打印十字图

问题描述

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

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

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
 
输出格式
对应包围层数的该标志。
 
样例输入1
1
 
样例输出1
 
提示
请仔细观察样例,尤其要注意句点的数量和输出位置
 
分析:其实就是一个模拟的水题,关键就是题目给的提示:观察位置!要是因为麻烦而写乱了那就等于不要这送分题啦。。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn = 100;
 7 int n;
 8 int a[maxn][maxn];
 9 void draw()
10 {
11     memset(a, 0, sizeof(a));
12     int t, tot=4*(n+1);
13     for(int i = 1; i <= n+1; i++)
14     {
15         t = i*2;
16         a[t][t] = 1;                    //a[0][2],a[1][2],a[2][2],a[2][1],a[2][0],
17         a[t][t-1] = a[t][t-2] = 1;
18         a[t-1][t] = a[t-2][t] = 1;
19         a[t][tot-t] = 1;                //a[0][14],a[1][14],a[2][14],a[2][15],a[2][16]
20         a[t][tot-t+1] = a[t][tot-t+2] = 1;
21         a[t-1][tot-t] = a[t-2][tot-t] = 1;
22         a[tot-t][t] = 1;                //a[14][0],a[14][1],a[14][2],a[15][2],a[16][2]
23         a[tot-t+1][t] = a[tot-t+2][t] = 1;
24         a[tot-t][t-1] = a[tot-t][t-2] = 1;
25         a[tot-t][tot-t] = 1;                //a[14][14],a[14][15],a[14][16],a[15][14],a[16][14]
26         a[tot-t+1][tot-t] = a[tot-t+2][tot-t] = 1;
27         a[tot-t][tot-t+1] = a[tot-t][tot-t+2] = 1;
28     }
29     for(int i = 1; i <= n+1; i++)
30     {
31         t = i*2;
32         for(int j = t; j <= tot-t; j++)
33         {
34             //cout << t-2 << " " << tot-t+2 << endl;
35             a[j][t-2] = a[j][tot-t+2] = 1;
36             a[t-2][j] = a[tot-t+2][j] = 1;
37         }
38 
39     }
40 }
41 void display()
42 {
43     for(int i = 0; i < 4*(n+1)+1; i++)
44     {
45         for(int j = 0; j < 4*(n+1)+1; j++)
46         {
47             if(a[i][j]) putchar('$');
48             else putchar('.');
49         }
50         putchar('
');
51     }
52 }
53 int main()
54 {
55     scanf("%d", &n);
56 
57     draw();
58 
59     display();
60 
61     return 0;
62 }
View Code
代码:
 
原文地址:https://www.cnblogs.com/LLGemini/p/4410286.html