使用 for 循环编写一个 C++ 程序来打印空心左侧帕斯卡星形三角形模式。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Hollow Left Pascals Star Triangle Row = ";
cin >> rows;
cout << "Hollow Left Pascals Star Triangle Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = rows; j > i; j--)
{
cout << " ";
}
for(k = 1; k <= i; k++)
{
if(k == 1 || k == i)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
for(i = 1; i <= rows - 1; i++)
{
for(j = 1; j <= i; j++)
{
cout << " ";
}
for(k = rows - 1; k >= i; k--)
{
if(k == rows - 1 || k == i)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
return 0;
}

这个C++ 示例使用 while 循环打印给定字符的空心左侧帕斯卡三角形。
#include<iostream>
using namespace std;
int main()
{
int i = 1, j, k, rows;
char ch;
cout << "Enter Hollow Left Pascals Star Triangle Row = ";
cin >> rows;
cout << "Enter Symbol for Hollow Left Pascals Triangle = ";
cin >> ch;
cout << "Hollow Left Pascals Star Triangle Pattern\n";
while(i <= rows)
{
j = rows;
while( j > i)
{
cout << " ";
j--;
}
k = 1;
while( k <= i)
{
if(k == 1 || k == i)
{
cout << ch;
}
else
{
cout << " ";
}
k++;
}
cout << "\n";
i++;
}
i = 1;
while( i <= rows - 1)
{
j = 1;
while( j <= i)
{
cout << " ";
j++;
}
k = rows - 1;
while( k >= i)
{
if(k == rows - 1 || k == i)
{
cout << ch;
}
else
{
cout << " ";
}
k--;
}
cout << "\n";
i++;
}
return 0;
}
Enter Hollow Left Pascals Star Triangle Row = 12
Enter Symbol for Hollow Left Pascals Triangle = $
Hollow Left Pascals Star Triangle Pattern
$
$$
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$$
$