打印左帕斯卡数三角的C++程序

60 阅读1分钟

编写一个C++程序,使用for循环打印左帕斯卡数的三角形。

#include using namespace std;

int main() { int i, j, k, rows;

cout << "Enter Left Pascal Number Triangle Row = ";
cin >> rows;

cout << "Left Pascals Number Triangle Pattern\\n"; 

for(i = 1; i <= rows; i++)
{
	for(j = i; j < rows; j++)
	{
        cout << "  ";
    }
    for(k = 1; k <= i; k++)
    {
        cout << k << " ";
    }
    cout << "\\n";
}	

for(i = rows; i >= 1; i--)
{
	for(j = i; j <= rows; j++)
	{
        cout << "  ";
    }
    for(k = 1; k < i; k++)
    {
        cout << k << " ";
    }
    cout << "\\n";
}

return 0;

这个C++例子使用while循环打印左帕斯卡数的三角形。

#include using namespace std;

int main() { int i, j, k, rows;

cout << "Enter Left Pascal Number Triangle Row = ";
cin >> rows;

cout << "Left Pascals Number Triangle Pattern\\n"; 

i = 1; 
while(i <= rows)
{
    j = i;
	while(j < rows)
	{
        cout << "  ";
        j++;
    }
    k = 1;
    while( k <= i)
    {
        cout << k << " ";
        k++;
    }
    cout << "\\n";
    i++;
}	

i = rows;
while( i >= 1)
{
    j = i;
	while( j <= rows)
	{
        cout << "  ";
        j++;
    }
    k = 1;
    while(k < i)
    {
        cout << k << " ";
        k++;
    }
    cout << "\\n";
    i--;
}

return 0;
Enter Left Pascal Number Triangle Row = 9
Left Pascals Number Triangle Pattern
                1 
              1 2 
            1 2 3 
          1 2 3 4 
        1 2 3 4 5 
      1 2 3 4 5 6 
    1 2 3 4 5 6 7 
  1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
  1 2 3 4 5 6 7 8 
    1 2 3 4 5 6 7 
      1 2 3 4 5 6 
        1 2 3 4 5 
          1 2 3 4 
            1 2 3 
              1 2 
                1