Processing math: 100%
0%

Codeforces 454A 解题报告

题意简述

给定一个整数 n(3n101n) ,要求按照规定输出一个 n×n 的矩阵(详见样例)。

时间限制:1s 空间限制:256MB

题目链接

Codeforces 454A

题解

观察样例可以发现,矩阵沿 n/2 行对称,第 i 行中的字符 D 与行中心字符的距离不超过 n/2n/2i,模拟打印整个矩阵即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int limit = n / 2;
for(int i = 0;i < n;i++) {
int nowMaxDisplayDis = limit - abs(limit - i);
for(int j = 0;j < n;j++) {
if(abs(limit - j) <= nowMaxDisplayDis) {
cout << "D";
}else {
cout << "*";
}
}
cout << endl;
}
return 0;
}