编写一个C++程序,用示例打印倒星形金字塔。此程序允许我们输入行数和要在倒金字塔模式中打印的符号。接下来,嵌套的 for 循环将在总行数和 1 之间迭代以打印 *(或给定的符号)。在这里,如果 k != (2 * i – 1) 条件为真,则打印 *,否则,C++ 打印空格。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
char ch;
cout << "\nPlease Enter Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "\nPlease Enter Any Symbol to Print = ";
cin >> ch;
cout << "\n-----Inverted Pyramid Star Pattern-----\n";
for ( i = rows ; i >= 1; i-- )
{
for ( j = 0 ; j <= rows - i; j++ )
{
cout << " ";
}
for(k = 0; k != (2 * i - 1); k++)
{
cout << ch;
}
cout << "\n";
}
return 0;
}

使用 while 循环打印倒星形金字塔的 C++ 程序
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
char ch;
cout << "\nPlease Enter Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "\nPlease Enter Any Symbol to Print = ";
cin >> ch;
cout << "\n-----Inverted Pyramid Star Pattern-----\n";
i = rows ;
while(i >= 1 )
{
j = 0 ;
while( j <= rows - i)
{
cout << " ";
j++ ;
}
k = 0;
while (k != (2 * i - 1))
{
cout << ch;
k++;
}
cout << "\n";
i--;
}
return 0;
}
Please Enter Inverted Pyramid Star Pattern Rows = 9
Please Enter Any Symbol to Print = #
-----Inverted Pyramid Star Pattern-----
#################
###############
#############
###########
#########
#######
#####
###
#