为什么VisualStudio和GCC为C++代码生成不同的输出 我有以下C++程序: #include "stdafx.h" #include <iostream> #include <fstream> #include <ostream> using namespace std; ifstream input("input.txt"); long long N, C, D; long long a[100000][4]; long long b[100000][4]; int case_number = 0; long long chosen_machine; long long max(long long v1, long long v2) { // returns the maximume value between 2 values if (v1 > v2) return v1; else return v2; } long long f_recursive(long long cash, long long i, long long i_chosen, int e) { if (i == N - 1) { if (e == 0) //we dont buy the current machine { return (cash + (D-b[i][0]+1)*b[i_chosen][3] ); } else //e==1 and we will buy the current machine { if (cash < b[i][1]) //we cant buy it { if (i_chosen == -1) return cash; else return (cash + (D - b[i][0] + 1)*b[i_chosen][3]); } cash = cash - b[i][1] + b[i][2]; return (cash + (D - b[i][0])*b[i][3]); } } if (e == 1) //we are going to buy the machine { if (cash < b[i][1]) { //we cant buy it so we keep the current cash and go to next machine return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } cash = cash - b[i][1] + b[i][2] + (b[i + 1][0] - b[i][0] - 1)*b[i][3]; return max(f_recursive(cash, i + 1, i, 1), f_recursive(cash, i + 1, i, 0)); } else // e==0 and we wont buy the machnie { if (i_chosen == -1) // if we did not buy any machine before (we still own no machine) { return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } cash = cash + (b[i + 1][0] - b[i][0])*b[i_chosen][3]; return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } } int read_case() { //reads each case and corresponding N C D and also the machines details to array a string line; input >> N >> C >> D; for (long long i = 0; i < N; i++) { input >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3]; } return 1; } int sort_a() { long long checked[100000]; for (long long i = 0; i < N; i++) { long long max = 999999999999999999; long long j_chosen; for (long long j = 0; j < N; j++) { if (a[j][0] <= max && checked[j]!=1) { max = a[j][0]; j_chosen = j; } } b[i][0] = a[j_chosen][0]; b[i][1] = a[j_chosen][1]; b[i][2] = a[j_chosen][2]; b[i][3] = a[j_chosen][3]; checked[j_chosen] = 1; } return 1; } int main() { int exit_condition = 0; while (!exit_condition) { read_case(); if (N == 0 && C == 0 && D == 0) { exit_condition = 1; continue; } if (N==0) // there is no machine, so simply output the dollar we have cout << "Case " << ++case_number << ": " << C << "\n"; else { sort_a(); cout << "Case " << ++case_number << ": " << max(f_recursive(C, 0, -1, 0), f_recursive(C, 0, -1, 1)) << "\n"; } } input.close(); cout << "Finished, please press Enter..."; getchar(); return 0; } 微软VisualStudioC++中,代码产生这个输出,它在调试或发布上这样做: Case 1: 44 Case 2: 1116877054 Case 3: 2353506445 Case 4: 10 Case 5: 5009 Case 6: 9909 Case 7: 10 Case 8: 10 Case 9: 11 Case 10: 999999999999999999 Case 11: 87 Case 12: 200 Case 13: 567 Case 14: 87 Finished, please press Enter..

为什么VisualStudio和GCC为C++代码生成不同的输出 我有以下C++程序: #include "stdafx.h" #include <iostream> #include <fstream> #include <ostream> using namespace std; ifstream input("input.txt"); long long N, C, D; long long a[100000][4]; long long b[100000][4]; int case_number = 0; long long chosen_machine; long long max(long long v1, long long v2) { // returns the maximume value between 2 values if (v1 > v2) return v1; else return v2; } long long f_recursive(long long cash, long long i, long long i_chosen, int e) { if (i == N - 1) { if (e == 0) //we dont buy the current machine { return (cash + (D-b[i][0]+1)*b[i_chosen][3] ); } else //e==1 and we will buy the current machine { if (cash < b[i][1]) //we cant buy it { if (i_chosen == -1) return cash; else return (cash + (D - b[i][0] + 1)*b[i_chosen][3]); } cash = cash - b[i][1] + b[i][2]; return (cash + (D - b[i][0])*b[i][3]); } } if (e == 1) //we are going to buy the machine { if (cash < b[i][1]) { //we cant buy it so we keep the current cash and go to next machine return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } cash = cash - b[i][1] + b[i][2] + (b[i + 1][0] - b[i][0] - 1)*b[i][3]; return max(f_recursive(cash, i + 1, i, 1), f_recursive(cash, i + 1, i, 0)); } else // e==0 and we wont buy the machnie { if (i_chosen == -1) // if we did not buy any machine before (we still own no machine) { return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } cash = cash + (b[i + 1][0] - b[i][0])*b[i_chosen][3]; return max(f_recursive(cash, i + 1, i_chosen, 1), f_recursive(cash, i + 1, i_chosen, 0)); } } int read_case() { //reads each case and corresponding N C D and also the machines details to array a string line; input >> N >> C >> D; for (long long i = 0; i < N; i++) { input >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3]; } return 1; } int sort_a() { long long checked[100000]; for (long long i = 0; i < N; i++) { long long max = 999999999999999999; long long j_chosen; for (long long j = 0; j < N; j++) { if (a[j][0] <= max && checked[j]!=1) { max = a[j][0]; j_chosen = j; } } b[i][0] = a[j_chosen][0]; b[i][1] = a[j_chosen][1]; b[i][2] = a[j_chosen][2]; b[i][3] = a[j_chosen][3]; checked[j_chosen] = 1; } return 1; } int main() { int exit_condition = 0; while (!exit_condition) { read_case(); if (N == 0 && C == 0 && D == 0) { exit_condition = 1; continue; } if (N==0) // there is no machine, so simply output the dollar we have cout << "Case " << ++case_number << ": " << C << "\n"; else { sort_a(); cout << "Case " << ++case_number << ": " << max(f_recursive(C, 0, -1, 0), f_recursive(C, 0, -1, 1)) << "\n"; } } input.close(); cout << "Finished, please press Enter..."; getchar(); return 0; } 微软VisualStudioC++中,代码产生这个输出,它在调试或发布上这样做: Case 1: 44 Case 2: 1116877054 Case 3: 2353506445 Case 4: 10 Case 5: 5009 Case 6: 9909 Case 7: 10 Case 8: 10 Case 9: 11 Case 10: 999999999999999999 Case 11: 87 Case 12: 200 Case 13: 567 Case 14: 87 Finished, please press Enter..,c++,gcc,C++,Gcc,在Linux gcc g++上,它生成以下输出: Case 1: 44 Case 2: 1116877054 Case 3: 2353506445 Case 4: 10 Case 5: 10 Case 6: 10 Case 7: 10 Case 8: 10 Case 9: 10 Case 10: 1991568403 Case 11: 10 Case 12: 10 Case 13: 10 Case 14: 10 Finished, please press Enter.. 作为参考,我试图解决

在Linux gcc g++上,它生成以下输出:

Case 1: 44
Case 2: 1116877054
Case 3: 2353506445
Case 4: 10
Case 5: 10
Case 6: 10
Case 7: 10
Case 8: 10
Case 9: 10
Case 10: 1991568403
Case 11: 10
Case 12: 10
Case 13: 10
Case 14: 10
Finished, please press Enter..
作为参考,我试图解决这个问题:


有人知道或知道为什么在不同的编译器上输入和输出会不同吗?

在visual studio中,当创建变量时,其值默认为零,但在Linux g++中,变量将是随机的。因此,分配变量将解决此问题。这就是我遇到的问题,但是,基于代码还有其他问题。

可能依赖于未定义的行为或实现定义的行为。但我一直没能看到它;这里发布了很多代码,很难理解为什么不同编译器下的结果不同,最简单的答案是:代码中的某个地方存在未定义的行为。例如:在某些条件下,在分配值之前,可能会使用排序中选择的j_。2您是否尝试过使用调试器单步检查代码,以调查是否一切都按照您的期望工作?签入排序a未初始化。不相关。您经常使用int,至少对读者来说,bools更有意义。布尔表示我是真是假。而int可以是任何东西。long-long-checked[100000]={0};将每个元素初始化为0,而不是未知值,这也需要像那样在堆栈上放置大量内存。哪个变量?全局变量总是初始化为其默认值,对于int类型为0。对于任何其他内容,都没有保证,并且在调试模式下Visual Studio使用特殊值进行初始化,因此我希望您的行为在调试和发布之间有所不同。根据我的经验,在Visual Studio中创建时,数组int将初始化为0,但是在Linux中,数组的元素将是随机的。通常,始终为定义的变量指定一个值;根据你所经历的与C++标准的保证是不一样的。在使用Visual Studio的调试生成中,调试分配器将为未初始化的变量分配特殊的sentinel值。您没有回答以下问题:为了使程序正确运行,您初始化了哪个变量?如果没有细节,你自己回答的问题对任何人都没有帮助。
Case 1: 44
Case 2: 1116877054
Case 3: 2353506445
Case 4: 10
Case 5: 10
Case 6: 10
Case 7: 10
Case 8: 10
Case 9: 10
Case 10: 1991568403
Case 11: 10
Case 12: 10
Case 13: 10
Case 14: 10
Finished, please press Enter..