实现效果如下:
下面是实现方法:
# include <iostream>
using namespace std;
int main()
{
int n, up, down;
cout << "输入菱形总行数n:" << endl;
cin >> n;
up = n / 2 + 1; //上半部分行数
down = n / 2; //下半部分行数
for (int i = 1; i <= up; i++) //上半部分
{
for (int j = 1; j <= (up - i); j++) cout << ' '; //"up - i"当前行第一个星号左边的空格数
for (int g = 1; g <= (2 * i - 1); g++) cout << '*'; //"2 * i - 1"当前行的星号数
cout << endl;
}
for (int i = down; i >= 1; i--) //下半部分
{
for (int j = 1; j <= (down - i + 1); j++) cout << ' ';//"down - i + 1"当前行第一个星号左边的空格数
for (int g = 1; g <= (2 * i - 1); g++) cout << '*'; //"2 * i - 1"当前行的星号数
cout << endl;
}
system("pause");
return 0;
}
先介绍到这里,欢迎大家在评论区留言~