CPP和Java提供不同的输出

时间:2017-05-05 22:38:23

标签: java c++

我有两组代码。一个在CPP中,另一个在java中。两组代码看起来都是一样的,但它们给了我不同的输出。我在这里完全糊涂了。我是cpp的新手。请帮忙。

CPP代码:

#compare df['0'] to 10 and convert the results to int and assign it to df['1']
df['1'] = (df['0']<10).astype(int)

df
Out[1287]: 
    0  1
0   3  1
1  11  0
2   7  1
3  15  0

输出:184 链接:http://cpp.sh/2xeee

Java代码:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

int helper(vector<vector<int> > table, int i, int j) {
    if ( i < 0 || j < 0 || i >= 4 || j >= 4 ) return 0;
    if ( table[i][j] == 1 ) return 0;
    if ( i == 3 && j == 3 ) return 1;

    table[i][j] = 1;
    return helper(table, i, j+1) + helper(table, i+1, j) + helper(table, i, j-1) + helper(table, i-1, j);
}

int main(int argc, char *argv[]) {
    vector<vector<int> > table;
    vector<int> x(4,0);
    for (int i = 0; i < 4; ++i)
        table.push_back(x);


    cout << helper(table, 0, 0) << endl;

    return 0;
}

输出:2 链接:http://ideone.com/e.js/U9X1FX

1 个答案:

答案 0 :(得分:3)

在java中,表通过引用移交给帮助程序。在C ++中,每次调用都会复制表。

要使C ++的行为与java类似,请更改帮助程序调用以获取引用:

BigInteger

编辑:要使Java的行为与原始C ++代码相似,您需要复制表中受影响的部分。将助手更改为:

int helper(vector<vector<int> >& table, int i, int j) {