我有以下C ++程序:
tail
我使用它作为输入(在文件“input.txt”中):
#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;
}
在Microsoft Visual Studio(C ++)中,代码生成此输出(它在Debug或Release上执行此操作):
6 10 20
6 12 1 3
1 9 1 2
3 2 1 2
8 20 5 4
4 11 7 4
2 10 9 1
4 979142410 1000000000
719554784 535235252 43212142 2
679922882 929213070 72282699 3
947550084 114776168 96759913 1
811283493 865444308 248312924 4
10 936980155 1000000000
762761567 683459202 109944378 6
686089795 734650986 45620682 4
678254540 925086416 24622261 5
807652386 596279745 140024880 9
791494729 585405092 50583956 8
655401693 691328196 82491162 3
799450676 654579438 65612488 10
580278950 624801431 68664589 2
834332323 692933073 524827737 7
101802074 760182347 3637050 1
1 10 100
50 11 9 100
1 10 100
50 10 9 100
1 10 100
1 10 9 100
1 10 100
100 10 9 100
1 10 100
95 10 1 1
1 10 100
95 10 1 2
1 1000000000 1000000000
1 10 9 1000000000
2 10 100
80 67 60 7
20 8 5 1
2 10 100
80 66 60 7
20 8 5 1
2 10 100
80 66 60 1
20 8 5 7
2 10 30
20 8 5 8
20 2 1 7
0 0 0
在Linux(gcc g ++)上,它产生这个输出:
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..
作为参考,我试图解决这个问题: http://docdro.id/H6fd5bs
有人知道或不知道输入和输出在不同的编译器上会有什么不同吗?
答案 0 :(得分:-1)
在visual studio中创建变量时,默认情况下其值为零,但在Linux g ++中,变量将是随机的。因此,分配变量将解决这个问题。这是我遇到的问题,但是,基于代码会有其他问题。