简体   繁体   English

C ++:Pascal的三角形 - 奇怪的结果

[英]C++: Pascal's triangle - weird results

This is my first question so don't be mad at me, if I did something wrong. 这是我的第一个问题所以如果我做错了,不要生我的气。 I have to make a C++ program which returns an element from a selected row, for example: 我必须创建一个C ++程序,它返回一个选定行的元素,例如:

Triangle 4 0 1 2 3

should return elements: 0, 1, 2 and 3 from row number 4, but it returns strange things, like: 应该从第4行返回元素: 0, 1, 2 and 3 ,但它返回奇怪的东西,例如:

Element 0: 1
Element 1: 10179988
Element 2: 50792126
Element 3: 91425820

I have no idea why 我不知道为什么
Here's my code: 这是我的代码:

#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Pascal {
    private:
        int *tab;

    public:
        Pascal(int n) throw(string) {
            if (n < 0)
                throw (string)"";

            tab = new int[n+1];

            for(int i = 1; i <= n; i++) {
                for(int k = i; k >=0; k--) {
                    if (k - 1 >= 0)
                        tab[k] += tab[k-1];
                    else
                        tab[k] = 1;
                }
            }
        };

    int element(int m) {
        return tab[m];
    }
};

int main(int argc, char* argv[]) {
        int n = 0, m = 0, elem = 0;

        try {
            n = strtol(argv[1], NULL, 0);
            Pascal *row;

            for(int i = 2; i < argc; i++) {
                try {
                    m = strtol(argv[i], NULL, 0);
                    row = new Pascal(n+1);

                    if (m <= n && m >= 0) {
                        elem = row->element(m);
                        cout << "Element " << m << ": "<< elem << endl;
                    } else
                        cout << m << " - bad element index" << endl;

                } catch (string ex) {
                    cout << argv[i] << " - bad element index!" << endl;
                    continue;
                }

                delete[] row;
            }
        } catch (string e) {
            cout << argv[1] << " - bad row index!" << endl;
            return 0;
        }
    }

I'll be grateful for any answer 我会感激任何答案

           tab = new int[n+1];

            for(int i = 1; i <= n; i++) {
                for(int k = i; k >=0; k--) {
                    if (k - 1 >= 0)
                        tab[k] += tab[k-1];
                    else
                        tab[k] = 1;
                }
            }

first iteration:  i=1, k=1, tab[1]+=tab[0];
second iteration: i=1, k=2, tab[2]+=tab[1]; 

So you are not properly initializing your array, you are simply adding whatever values are in memory... 所以你没有正确初始化你的数组,你只是添加内存中的任何值...

I think replacing if (k - 1 >= 0) with if (k - 1 > 0) should solve your problem 我认为用if (k - 1 >= 0)替换if (k - 1 >= 0) if (k - 1 > 0)可以解决你的问题

Try 尝试

tab = new int[n+1];

for(int i = 0; i <= n; i++) {
    tab[i] = 1;
    for(int k = i; --k > 0; )
        tab[k] += tab[k-1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM