编写一个 C++ 程序,使用 for 循环打印空心倒星形金字塔图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Hollow Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "Hollow Inverted Pyramid Star Pattern\n";
for(i = rows; i > 0; i--)
{
for(j = 1; j <= rows - i; j++)
{
cout << " ";
}
for(k = 1; k <= 2 * i - 1; k++)
{
if(i == 1 || i == rows || k == 1 || k == 2 * i - 1)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
return 0;
}

此 C++ 示例 使用 while 循环打印给定字符的空心倒金字塔图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
char ch;
cout << "Enter Hollow Inverted Pyramid Star Pattern Rows = ";
cin >> rows;
cout << "Enter Symbol for Hollow Inverted Pyramid Pattern = ";
cin >> ch;
cout << "Hollow Inverted Pyramid Star Pattern\n";
i = rows;
while( i > 0)
{
j = 1;
while(j <= rows - i)
{
cout << " ";
j++;
}
k = 1;
while( k <= 2 * i - 1)
{
if(i == 1 || i == rows || k == 1 || k == 2 * i - 1)
{
cout << ch;
}
else
{
cout << " ";
}
k++;
}
cout << "\n";
i--;
}
return 0;
}
Enter Hollow Inverted Pyramid Star Pattern Rows = 15
Enter Symbol for Hollow Inverted Pyramid Pattern = #
Hollow Inverted Pyramid Star Pattern
#############################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#