Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!not-for-mail From: jt@toerring.de (Jens Thoms Toerring) Newsgroups: comp.programming Subject: Re: SPOJ Problem HOTELS Date: 11 Jun 2013 21:36:41 GMT Organization: Freie Universitaet Berlin Lines: 80 Message-ID: References: X-Trace: news.uni-berlin.de VgP0K8ZO2b/y4ruhOinKDAMaVddIVLCuwG07n6bv9yQU0+ X-Orig-Path: not-for-mail User-Agent: tin/1.9.3-20080506 ("Dalintober") (UNIX) (Linux/2.6.30-1-amd64 (x86_64)) Xref: csiph.com comp.programming:3424 singh13490mayank@gmail.com wrote: > I am getting WA to spoj problem code HOTELS, Plz help me What the hell is that for an error? What's 'WA', what's a 'spoj problem' and what is 'code HOTEL'? > here's my code I'm guessing that it's supposed to be C, am I correct? > #include > #include You're not using anything that would require > int main() Make that int main( void ) if it's supposed to be C. > { > long N,M,sum,max; > long i,j; > scanf("%ld",&N); > scanf("%ld",&M); It might be advisable to check the return value of scanf() - if the input doesn't match what scanf() is told to expect you'll be in for some surprises... > long arr[N]; Note that this will only work with C99. > for(i=0;i { > scanf("%ld",&arr[i]); > } > max = 0; > sum = 0; > j = 0; > for(i=0;i<=N;i++) > { > while(sum>M) > { > sum = sum-arr[j]; > j++; This might increment 'j' past the last index of 'arr' and then all bets are off. Rather likely you should stop the while loop if 'j' has reached 'N', shouldn't you? > } > if(sum == M) > { > printf("%ld",sum); > exit(0); > } > if(sum < M&&i { > sum = sum+arr[i]; > if(sum<=M) > max = sum; > } > } > printf("%ld",max); > return 0; > } Beside the question what all this 'WA', spoj' and 'HOTEL' stuff is about what is the purpose of this program and what is it's typical input? Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de