#include
using namespace std;
int main()
{
int num;
for(num=1;num<10;num++)
{
int i;
int n=num;
if(n>5)
{
n=10-n;
}
//打印空格
for(i=0;i<5-n;i++)
{
cout<<" ";
}
//打印星号
for(i=0;i<2*n-1;i++)
{
cout<<"* ";
}
cout<}
return 0;
}
结果如下:
//个数:9 - 2 | X - 5 |
// 开始位置: | 5 - X |
int i = 1, j = 0, k = 0, l = 0;
while (i <= 9)
{
j = abs(5 - i);
k = 9 - j;
l = 0;
while (l <= j)
{
cout << " ";
l++;
}
while (l <= k)
{
cout << "*";
l++;
}
cout << endl;
i++;
}