Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #3424
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: SPOJ Problem HOTELS |
| Date | 2013-06-11 21:36 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <b1pjj9Fbe8oU1@mid.uni-berlin.de> (permalink) |
| References | <ba7787c1-a003-4d07-97a1-f6ef331e85fe@googlegroups.com> |
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 <stdio.h>
> #include <stdlib.h>
You're not using anything that would require <stlib.h>
> 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<N;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<N)
> {
> 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
Back to comp.programming | Previous | Next — Previous in thread | Find similar | Unroll thread
SPOJ Problem HOTELS singh13490mayank@gmail.com - 2013-05-31 09:26 -0700 Re: SPOJ Problem HOTELS jt@toerring.de (Jens Thoms Toerring) - 2013-06-11 21:36 +0000
csiph-web