You are not logged in. Please login at www.codechef.com to post your questions!

×

Why I'm getting WA with this code on The Lead Game problem ????

#include <stdio.h>
#include <math.h>
int main(void) {
int n,i=0,max,winner;
scanf("%d",&n);
int S[n],T[n],R[n],L[n];
while(n--){
    scanf("%d",&S[i]); scanf("%d",&T[i]);
    L[i] = abs(S[i]-T[i]);
    if (S[i]>T[i])
        R[i]=1;
    else
        R[i]=2;
    i++;
}
max = L[0];
winner = R[0];
for (i=1;i<n;++i){
    if (L[i]>max)
        max = L[i];
        winner = R[i];
}
printf("%d %d",winner,max);
return 0;
}

asked 20 May '15, 12:56

edwinharly's gravatar image

2★edwinharly
132
accept rate: 0%


link

answered 20 May '15, 13:01

anupam_datta's gravatar image

4★anupam_datta
469529
accept rate: 7%

So, where is the fault of my code ??? I don't see any of it. Just a different way to solve the problem

(21 May '15, 08:31) edwinharly2★

OK I need the karma. Here is an accepted version of your code,
http://www.codechef.com/viewsolution/6988064
There are three primary errors which I will point out.

int S[n],T[n]
You don't need to store the values why use array.
Simply replace this with
int S,T;



scanf("%d",&S[i]); scanf("%d",&T[i]);
The scores are cumulative, read the question again. Score of the previous round questions get carried forward. Replace this with,

int a,b;
scanf("%d",&a); scanf("%d",&b);
S=S+a;
T=T+b;


while(n--)
You are reducing the value of n here to 0, how do you expect the next for loop to work,
for (i=1;i<n;++i)

if (L[i]>max)
max = L[i];
winner = R[i];

You missed a bracket here. Without it winner is always getting set to the R[n-1]
if (L[i]>max){
max = L[i];
winner = R[i];
}

Hope this helps.

link

answered 21 May '15, 11:37

mehwhatever's gravatar image

2★mehwhatever
161
accept rate: 50%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,766
×2,718
×1,388
×1,070
×847
×56

question asked: 20 May '15, 12:56

question was seen: 941 times

last updated: 22 May '15, 12:43