【问题标题】:How to integrate a row in pascal's triangle?如何在帕斯卡三角形中整合一行?
【发布时间】:2019-04-07 22:29:09
【问题描述】:

我有一个最大行数为 5 的帕斯卡三角形。假设我想找到第四行的积分。如何访问帕斯卡三角形中的第四行。 More precisely I want to know how to access a row in the pascal's triangle by entering the number n of the row

代码

#include <iostream>

using namespace std;

int main(){

    int a1, a2, a3, a4, a5, pascal, columns;
    const int rows = 5;

    int **array = new int *[rows]; //generating array
    for(int i = 0; i <= rows; i++)
    array[i] = new int [columns];

    for (int i = 0; i <= rows; i++){  //loop for te pascal's triangle
        for (int j = 0; j <= i; j++){
            if(j == 0 || i == 0){
                pascal = 1;  //first element of pascal's triangle
            }
            else{
                pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
            }
            cout << "  " << pascal; // printing pascals triangle
         }
         cout << "\n";
    }
    cout << "enter which row to integrate: ";
    // here I want to directly access a row rather that entering the elements of the row 
    cin >> a1;
    cin >> a2;
    cin >> a3;
    cin >> a4;
    cin >> a5;

 }

1
1 1
1 2 1
1 3 3 1 ------> 喜欢n = 4 我想整合这一行的元素
1 4 6 4 1

答案应该是for 1,3,3,1 = 0, 1, 1.5, 1, 0.25

【问题讨论】:

  • columns 未初始化,它的值可以是任何值。而for(int i = 0; i &lt;= rows; i++) 是越界访问。 array[rows-1] 是最大的可访问元素。
  • 它有什么改变吗?
  • 是的。您的数组中有多少列? 0 或 -10 列好吗?将columns 用作数组长度时的值可以是anything。在最好的情况下,越界访问数组将读取下一条数据(例如,array[3][5] 可以读取array[4][0] 处的值)。在最坏的情况下,您将拥有nasal demons
  • 由于@Yksisarvinen 指出的错误,该程序具有未定义的行为。首要任务应该是摆脱任何 UB。只要程序有 UB,就不可能推断它的行为,因为它没有定义的行为。

标签: c++ arrays c++11 visual-c++ multidimensional-array


【解决方案1】:

你应该首先用元素填充数组,然后你可以像这样访问它们(编辑:确保初始化列变量,我将它设置为 5)

#include <iostream>

using namespace std;

int main() {

    int row_nb, pascal, columns = 5; //Initialized columns with columns = 5 
    const int rows = 5;

    int **array = new int *[rows]; //generating array
    for (int i = 0; i <= rows; i++)
        array[i] = new int[columns];

    for (int i = 0; i <= rows; i++) {  //loop for te pascal's triangle
        for (int j = 0; j <= i; j++) {
            if (j == 0 || i == 0) {
                pascal = 1;  //first element of pascal's triangle
            }
            else {
                pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
            }
            array[i][j] = pascal; //fill the array
            cout << "  " << pascal; // printing pascals triangle
        }
        cout << "\n";
    }
    cout << "enter which row to intergrate: ";
    // here I want to directly access a row rather that entering the elements of the row 
    cin >> row_nb; //input the row you want to access

    for (int i = 0; i <= row_nb; i++) { //access the elements in this row in the array
    cout << array[row_nb][i] << " ";
}
    return 0; // add the return statement since the return type of the main function is int
}

【讨论】:

  • 如果这对您有帮助,请考虑将其标记为已接受的答案;如果没有,请告诉我更多关于您的问题,也许我可以提供更多帮助:)
  • row_nb 的计数应该从 0 开始,因此通过输入 row_nb = 3 ,我得到 1,3,3,1 但我只得到 1,3,3 @Mahmoud Dafer
  • @Mahmoud Dafer 你能再帮我解决一个问题吗?
【解决方案2】:

正如我所说的when you last asked this

可以在计算时将每一行简单地存储到std::vector&lt;int&gt; 中(作为打印的补充或替代),然后将行列表保存在std::vector&lt;std::vector&lt;int&gt;&gt; 中。然后,要在计算三角形后访问第 n 行,您只需获取第二个向量的 nth 元素。

【讨论】:

  • 这不是它的工作原理。您拥有所有需要的信息。 编辑你的代码。
猜你喜欢
  • 2012-10-24
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2014-11-12
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多