问题 D: 沙漏图形 tri2str [1*+]
时间限制 : 1.000 sec 内存限制 : 128 MB
题目描述
问题:输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例
输入样例:
3
输出样例:
* * *
* *
*
* *
* * *
数据规模 1<= n <=50
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
cout << " ";
}
for (int j = 0; j < n - i; ++j) {
if (j == n - i - 1)
cout << "*";
else
cout << "*" << " ";
}
cout << endl;
}
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < i - 1; ++j) {
cout << " ";
}
for (int j = 0; j < n - i + 1; ++j) {
if (j == n - i)
cout << "*";
else
cout << "*" << " ";
}
cout << endl;
}
return 0;
}