Total Submissions: 1644 Accepted: 698 Creator: hhzhou
Problem Description
拟用setw、cout和for循环编写程序,打印输出“输出样例”中的图形。请完善下面的程序:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
//你的代码将被嵌在这里
return 0;
}
Input Description
从键盘录入一个正整数n,用于表示输出图形的行数
Output Description
按格式输出n行星号图形
Sample Input
4Sample Output
* *** ***** *******
cin >> n;
int i, j, k, m, u;
for (j = 1, i = n; j <= n; j++, i--)
{
k = i;
m = j;
u = j;
while (--k)
{
cout << " ";
}
while (u--)
{
cout << "*";
}
while (--m)
{
cout << "*";
}
cout << "\n";
}
或者
cin >> n;
int m, k;
for (int i = 1, j = n; i <= n; i++, j--)
{
cout << setw(j);
m = i;
k = i;
while (m--)
{
cout << "*";
}
while (--k)
{
cout << "*";
}
cout << "\n";
}