C++ 编程题:输出空心倒三角形 - 算法详解
思路:和输出实心三角形类似,只需要再次利用双重循环,对于第一行和最后一行特殊处理,其余行只需要在第一个和最后一个位置输出 ' * ' 即可。
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i == n || i == 1 || j == 1 || j == i) {
cout << ' * ';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nyFE 著作权归作者所有。请勿转载和采集!