版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/84935537
1031. 等腰三角形
题目描述
输入一个正整数n,输出高为n的由*组成的等腰三角形。
输入
输入一个正整数。
输出
输出高为n的由*组成的等腰三角形。
样例输入
3
样例输出
*
***
*****
数据范围限制
1<=n<=20
C++ 代码
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int n;
cin >> n;
assert(1<=n && n<=20);
for(int row=1; row<=n; row++)
{
for(int col=1; col<=n+row-1; col++)
{
if (col <= n-row) // col from 1 to n-row
{
cout << " ";
}
else // col from n-row+1 to n+row-1
{
cout << "*";
}
}
cout << endl;
}
return 0;
}