c++ - Pascal 的三角形主要使用 C++ 中的函数

标签 c++ function pascals-triangle

我正在尝试编写一个代码来显示帕斯卡三角形。而不是将结果显示为: enter image description here

我的结果显示为
1
1 1
1 2 1
1 3 3 1

请帮我弄清楚如何修改它以获得实际的三角形。我不能使用数组和指针,因为我的课上还没有涉及到它们。这是我的代码:

#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; 
    } 
}

最佳答案

这是更新后的版本。适用于任何 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;
    } 
}

输出:

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      

更新更紧密的三角形:

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;
    } 
}

输出

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          

关于c++ - Pascal 的三角形主要使用 C++ 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898756/

相关文章:

java - 帕斯卡三角 - 算法在 C++ 中有效,但在 Java 中得到 'Time Limit Exceeded'

c++ - 使用 Win32 进行组织的典型约定是什么?

c++ - 分配自身时的复制分配运算符

c++ - 如果构建是源外的,CMake 无法找出头文件依赖项?

c++ - 通过 or-ing 其他枚举器生成的枚举器值

r - 使用变量标签作为标题和轴标题时自动化 ggplots

go - 使用 big.Int int 编写 Pascal 的三角形

c++ - 按以字符串形式给出的名称调用函数

c - 返回字符串的函数的意外输出

java - 打印帕斯卡三角形的更多递归方法