0%

蓝桥杯PREV02-打印十字图

题目链接

题目概述

根据输入层数 n 和输出图案找规律。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
n=1:
. . $ $ $ $ $ . .
. . $ . . . $ . .
$ $ $ . $ . $ $ $
$ . . . $ . . . $
$ . $ $ $ $ $ . $
$ . . . $ . . . $
$ $ $ . $ . $ $ $
. . $ . . . $ . .
. . $ $ $ $ $ . .

n=3:
. . $ $ $ $ $ $ $ $ $ $ $ $ $ . .
. . $ . . . . . . . . . . . $ . .
$ $ $ . $ $ $ $ $ $ $ $ $ . $ $ $
$ . . . $ . . . . . . . $ . . . $
$ . $ $ $ . $ $ $ $ $ . $ $ $ . $
$ . $ . . . $ . . . $ . . . $ . $
$ . $ . $ $ $ . $ . $ $ $ . $ . $
$ . $ . $ . . . $ . . . $ . $ . $
$ . $ . $ . $ $ $ $ $ . $ . $ . $
$ . $ . $ . . . $ . . . $ . $ . $
$ . $ . $ $ $ . $ . $ $ $ . $ . $
$ . $ . . . $ . . . $ . . . $ . $
$ . $ $ $ . $ $ $ $ $ . $ $ $ . $
$ . . . $ . . . . . . . $ . . . $
$ $ $ . $ $ $ $ $ $ $ $ $ . $ $ $
. . $ . . . . . . . . . . . $ . .
. . $ $ $ $ $ $ $ $ $ $ $ $ $ . .

题目分析

找规律题,由于图像具有对称性,只需要求出左上角1/4部分,就能得到整个图案。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdio>
#include <iostream>
#define IOS ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
char map[135][135]; // 1/4的画
#define draw(x, y) map[y][x]='$'
#define out(x, y) cout<<map[y][x]
int n, len;
int main(){
IOS;
cin>>n;
len=(4*n+5+1)/2; // half length
int c=len-1;
for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
map[i][j]='.';
while(c>0){
draw(c,c);
draw(c-1, c); draw(c, c-1);
draw(c-2, c); draw(c, c-2);
for(int i=1; i+c<len; i++){
draw(c-2, c+i);
draw(c+i, c-2);
}
c-=2;
}

for(int i=0; i<len; i++){ //y
for(int j=0; j<len; j++) //x
out(j, i);
for(int j=len-2; j>=0; j--)
out(j, i);
cout<<endl;
}
for(int i=len-2; i>=0; i--){ //y
for(int j=0; j<len; j++) //x
out(j, i);
for(int j=len-2; j>=0; j--)
out(j, i);
cout<<endl;
}
return 0;

}

自我总结

比较无脑,但是不能眼花、浮躁。