2018HPU暑期集训第四次积分训练赛 K - 方框 题解(图形打印)
思路分析:题目已经明确透露了这道题的解法:就是画框。当 输入的边长 n - 4 > 0 的话,就表示可以在内层继续嵌套一个方框。废话就不多说了,直接上代码吧!
代码如下:
-
#include <iostream>
-
#include <algorithm>
-
using
namespace
std;
-
-
int n;
-
char ch[
105][
105];
-
-
int main() {
-
while (
cin >> n) {
-
// 初始化 (切记 ,必须要初始化!!!)
-
for (
int i =
0; i < n; i++)
-
for (
int j =
0; j < n; j++)
-
ch[i][j] =
' ';
-
-
int a =
0;
-
int b = n;
-
// 画框
-
do {
-
for (
int i = a; i < n - a; i++) {
// 画 上下 两条边
-
ch[a][i] =
'*';
-
ch[n - a -
1][i] =
'*';
-
}
-
for (
int i = a +
1; i < n - a -
1; i++) {
// 画 左右 两条边
-
ch[i][a] =
'*';
-
ch[i][n - a -
1] =
'*';
-
}
-
a +=
2;
-
b = b -
4;
-
}
while (b >
0);
// 判断是否继续画框的条件
-
// 输出
-
for (
int i =
0; i < n; i++) {
-
for (
int j =
0; j < n; j++) {
-
cout << ch[i][j];
-
}
-
cout <<
endl;
-
}
-
}
-
return
0;
-
}