daveo
1
Why does this code for the lead game give me a garbage value output. Also i wanted to know if the code is correct
#include
using namespace std;
int main()
{
int m,i,a[20],n,l[10],h,large,j;
cin>>m;
n=m*2;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(j=0;j<m;j+=2)
{
if(a[j]>a[j+1])
{
cout<<“1”;
l[j]=a[j]-a[j+1];}
else
{cout<<“2”;
l[j]=a[j+1]-a[j];}
}
l[h]=large;
for(h=0;h<m;h+=2)
{
if(l[h+2]>l[h])
l[h+2]=large;
}
cout<<large;
return 0;
}
neal95
2
Here you are not storing any value in “int large” …
so some garbage value is there in large when it was declared,and you are printing that value only.
You can overcome this by changing
“l[h+2]=large”
to
“large=l[h+2]”
This approach is not correct …
For the given testcase, your code may work…but it is not correct.
here you are storing the lead values of each round in the array l[].
Those lead values are actually not “a[j]-a[j+1]”…it is actually
the sum of the scores of the player1 upto the "i"th round - the sum of the scores of the player2 till "i"th round…
if you want better approach you may find my answer here,
http://www.codechef.com/viewsolution/4324967
if this answer helps…hope you can upvote and accept the answer…
All the Best
3 Likes