题目描述:
输入一个高度h,输出一个高为h,上底边为h的梯形。
输入:
一个整数h(1<=h<=1000)。
输出:
h所对应的梯形。
样例输入:
4
样例输出:
****
******
********
**********
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;
int main() {
char a[1001][1001] = {' '}; //为了方便 1-10这样计算
int h;
cin >> h;
int blowNumber = h + (h - 1) * 2; // 底边*的个数
int firstStarIndex = blowNumber - h + 1; //第一行 *的起始下标 // 10 - 4 = 6 --> 7-8-9-10
for(int i = 1; i <= h; i++) {
for (int j = firstStarIndex; j <= blowNumber; j++) {
a[i][j] = '*';
}
if (firstStarIndex >= 3) {
firstStarIndex -= 2;
}
}
for(int i = 1; i <= h; i++) {
for (int j = 1; j <= blowNumber; j++) {
cout << a[i][j];
}
cout << endl;
}
}
#include <iostream>
#include <algorithm>
using namespace std;
char maze[101][101];
int main() {
int h;
while (cin >> h ) {
int maxIndex = h + 2 * (h-1);
int should = h;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= maxIndex; j++) {
maze[i][j] = ' ';
}
}
for (int i = 1; i <= h; i++) {
for (int j = maxIndex; j >= maxIndex - should + 1; j--) {
maze[i][j] = '*';
}
should += 2;
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= maxIndex; j++) {
cout << maze[i][j];
}
cout << endl;
}
}
}