1

I was trying to write a code that would display pascals triangle. Instead of displaying the result as : enter image description here

my result is displayed as
1
1 1
1 2 1
1 3 3 1

Please help me figure out how to modify it to be able to get the actual triangle. I cant use arrays and pointers since those aren't covered in my class yet. Here's my code:

#include "stdafx.h"
#include <iostream> 
using namespace std; 
void PascalsTriangle(int);

int main() 
{   
    int n;
    cout << "Enter the number of rows you would like to print for Pascal's Triangle: "; 
    cin >> n;
    PascalsTriangle(n);
    return 0; 
}

void PascalsTriangle (int n){
    int i,j,x;  
    for(i=0;i<n;i++) 
    { 
        x=1; 
        for(j=0;j<=i;j++) 
        { 
            cout << x << " "; 
            x = x * (i - j) / (j + 1); 
        } 
        cout << endl; 
    } 
}
4

5 回答 5

1

这是更新的版本。适用于任何n. 我添加了一个函数来返回数字中的位数,因为内部循环的每次迭代都需要计算。

#include <iostream>
#include <string>
using namespace std;
void PascalsTriangle(int);

int main()
{
    int n;
    cout << "Enter the number of rows you would like to print for Pascal's Triangle: ";
    cin >> n;
    cout << endl;
    PascalsTriangle(n);
    return 0;
}

int numdigits(int x)
{
    int count = 0;
    while(x != 0) {
        x = x / 10;
        ++count;
    }
    return count;
}

void PascalsTriangle (int n)
{
    int i, j, x, y, maxlen;
    string len;
    for(i = 0; i < n; i++) {
        x = 1;
        len = string((n-i-1)*(n/2), ' ');
        cout << len;
        for(j = 0; j <= i; j++) {
            y = x;
            x = x * (i - j) / (j + 1);
            maxlen = numdigits(x) - 1;
            if(n % 2 == 0)
                cout << y << string(n - 1 - maxlen, ' ');
            else {
                cout << y << string(n - 2 - maxlen, ' ');
            }
        }
        cout << endl;
    } 
}

OUTPUTS:

Enter the number of rows you would like to print for Pascal's Triangle: 3

  1  
 1 1  
1 2 1  

Enter the number of rows you would like to print for Pascal's Triangle: 6

               1      
            1     1      
         1     2     1      
      1     3     3     1      
   1     4     6     4     1      
1     5    10    10     5     1  

Enter the number of rows you would like to print for Pascal's Triangle: 9

                                1        
                            1       1        
                        1       2       1        
                    1       3       3       1        
                1       4       6       4       1        
            1       5      10      10       5       1        
        1       6      15      20      15       6       1        
    1       7      21      35      35      21       7       1        
1       8      28      56      70      56      28       8       1        

Enter the number of rows you would like to print for Pascal's Triangle: 12

                                                                  1            
                                                            1           1            
                                                      1           2           1            
                                                1           3           3           1            
                                          1           4           6           4           1            
                                    1           5          10          10           5           1            
                              1           6          15          20          15           6           1            
                        1           7          21          35          35          21           7           1            
                  1           8          28          56          70          56          28           8           1            
            1           9          36          84         126         126          84          36           9           1            
      1          10          45         120         210         252         210         120          45          10           1            
1          11          55         165         330         462         462         330         165          55          11           1      

UPDATE for a more tighter triangle:

void PascalsTriangle(int n)
{
    int i, j, x, y, maxlen;
    string len;
    for(i = 0; i < n; i++) {
        x = 1;
        if(n % 2 != 0)
            len = string((n-i-1)*(n/2), ' ');
        else
            len = string((n-i-1)*((n/2)-1), ' ');
        cout << len;
        for(j = 0; j <= i; j++) {
            y = x;
            x = x * (i - j) / (j + 1);
            maxlen = numdigits(x);
            if(n % 2 == 0)
                cout << y << string(n - 2 - maxlen, ' ');
            else {
                cout << y << string(n - 1 - maxlen, ' ');
            }
        }
        cout << endl;
    } 
}

OUTPUT

Enter the number of rows you would like to print for Pascal's Triangle: 3
  1  
 1 1  
1 2 1  

Enter the number of rows you would like to print for Pascal's Triangle: 6
          1    
        1   1    
      1   2   1    
    1   3   3   1    
  1   4   6   4   1    
1   5  10  10   5   1    

Enter the number of rows you would like to print for Pascal's Triangle: 9
                                1        
                            1       1        
                        1       2       1        
                    1       3       3       1        
                1       4       6       4       1        
            1       5      10      10       5       1        
        1       6      15      20      15       6       1        
    1       7      21      35      35      21       7       1        
1       8      28      56      70      56      28       8       1  

Enter the number of rows you would like to print for Pascal's Triangle: 12
                                                       1          
                                                  1         1          
                                             1         2         1          
                                        1         3         3         1          
                                   1         4         6         4         1          
                              1         5        10        10         5         1          
                         1         6        15        20        15         6         1          
                    1         7        21        35        35        21         7         1          
               1         8        28        56        70        56        28         8         1          
          1         9        36        84       126       126        84        36         9         1          
     1        10        45       120       210       252       210       120        45        10         1          
1        11        55       165       330       462       462       330       165        55        11         1          
于 2013-11-11T07:23:19.673 回答
1

尝试这个 :

#include <iostream>
using namespace std;
int main()
{
    int n,k,i,x;
    cout << "Enter a row number for Pascal's Triangle: ";
    cin >> n;
    for(i=0;i<=n;i++)
    {
        x=1;
        for(k=0;k<=i;k++)
        {        
            cout << x << " ";
            x = x * (i - k) / (k + 1);
        }
        cout << endl;
    }
    return 0;
}

编辑:

#include <iostream>
using namespace std;
int main()
{
    int n,coef=1,space,i,j;
    cout<<"Enter number of rows: ";
    cin>>n;
    for(i=0;i<n;i++)
    {
        for(space=1;space<=n-i;space++)
        cout<<"  ";
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            cout<<"    "<<coef;
        }
        cout<<endl;
    }
}
于 2013-11-11T04:26:50.130 回答
0

你期待这个结果吗?

1111

123

13

1

您需要在输出字符串的末尾添加 endl 或等价于 "\r\n" 的常量字符串,或者在输出中使用逗号,然后,例如:

“1111,123,13,1”

“1,11,121,1331”

于 2013-11-11T04:28:20.247 回答
0

如果您希望它看起来像一个“三角形”,即一个对称的等腰三角形,请为您的PascalTriangle函数尝试此代码。唯一的问题是,当你得到更大的数字时,它会破坏一些对称性,但最多 5 行它会正常工作。

void PascalsTriangle(int n)
{
    int i, j, x;
    string len;
    for(i = 0; i < n; i++)
    {
        x = 1;
        len = string(n - i - 1, ' ');
        cout << len;
        for(j = 0; j <= i; j++)
        {
            cout << x << " ";
            x = x * (i - j) / (j + 1);
        }
        cout << endl;
    } 
}

OUTPUT

Enter the number of rows you would like to print for Pascal's Triangle: 5
    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 

相对于:

Enter the number of rows you would like to print for Pascal's Triangle: 5
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
于 2013-11-11T04:52:28.077 回答
0
#include <iostream>

using namespace std;


const int MAXC = 13; //-- Max column constant
const int MAXF = 7; //-- Max row constant

int m, n, k, x, y; //-- Counters and accumulators for loops
int arrayTriangulo[MAXF][MAXC]; // Array that stores the values of the Pascal Triangle

int main() {

    m = (MAXC/2); // Middle Column
    k = (MAXC/2); // Middle row

    //-- 1.- Fill in the Array from Left to Right and from Top to Bottom
    //-- 2.- Fill in the Array starting from row 0 through 13 (MAXF)

    for ( x = 0; x < MAXF; x++ ) {

        n = 1;

        //-- 3.- Fill in the Array starting from Column 0 through 7 (MAXC)

        for ( y = 0; y < MAXC; y++ ) {

            //-- Assign 0 to the Array element that is not part of the triangle.

            arrayTriangulo[x][y] = 0;

            //-- 4.- If it is on the edges of the triangle assigns the value of "n". Which we initialize in 1.

            if (( y == m ) || ( y == k )) {
                arrayTriangulo[x][y] = n;           
            } 

            //-- 5.- For the rest of the internal values of the triangle other than the edges.
            //-- The sum of the value is assigned (upper left row -1) + (upper right row + 1)

            if ( ( x > 1 ) && ( x < MAXF ) && ( y < MAXC-1 ) ) {
                arrayTriangulo[x][y] = arrayTriangulo[x-1][y-1] + arrayTriangulo[x-1][y+1];             
            }

            //-- 6.- Finally Draw the Triangle by omitting the values at zero.

            if ( arrayTriangulo[x][y] > 0 )
                cout << arrayTriangulo[x][y] << "  ";
            else
                cout << "   ";

        } 

        cout << endl;
        m--;
        k++;
    }
    return 0;
}
于 2017-06-19T19:08:27.280 回答