空心字符等腰梯形Plus-题解

描述

输入三个正整数n,m,h,分别代表等腰梯形的上底,下底和高。
输出一个星号构成的空心等腰梯形。n<m,h<100。

输入描述

三个正整数n,m,h。n<m,h<100。数据保证每行的空格个数差为正整数个。

输出描述

一个空心等腰梯形。

代码

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, m, h;
cin >> n >> m >> h;

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;
}

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