空心等腰梯形-题解

描述

输入一个n,输出一个以n为上底边高为4的空心等腰梯形。

输入描述

一个正整数n。(n<=20)

输出描述

一个上底边为n,高为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
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, m, h;
cin >> n;
h = 4;
m = n+6;
if (h == 1) {
for (int i = 0; i < m; i++) {
cout << '*';
}
cout << endl;
return 0;
}

int a = (m - n) / (2 * (h - 1));
for (int i = 0; i < h; i++) {
int b = n + 2 * i * a;
int c = (m - b) / 2;

for (int j = 0; j < c; j++) {
cout << ' ';
}

if (i == 0 || i == h - 1) {
for (int j = 0; j < b; j++) {
cout << '*';
}
} else {
cout << '*';
for (int j = 0; j < b - 2; j++) {
cout << ' ';
}
cout << '*';
}
cout << endl;
}
return 0;
}

空心等腰梯形-题解
https://chenxi-tijie.pages.dev/2025/07/空心等腰梯形-题解/
作者
chenxi
发布于
2025年7月5日
许可协议