Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #3401 > unrolled thread

SPOJ Problem HOTELS

Started bysingh13490mayank@gmail.com
First post2013-05-31 09:26 -0700
Last post2013-06-11 21:36 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.programming


Contents

  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

#3401 — SPOJ Problem HOTELS

Fromsingh13490mayank@gmail.com
Date2013-05-31 09:26 -0700
SubjectSPOJ Problem HOTELS
Message-ID<ba7787c1-a003-4d07-97a1-f6ef331e85fe@googlegroups.com>
I am getting WA to spoj problem code HOTELS, Plz help me

here's my code

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    long N,M,sum,max;
    long i,j;
    scanf("%ld",&N);
    scanf("%ld",&M);
    long arr[N];
    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++;
        }
        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;
}

[toc] | [next] | [standalone]


#3424

Fromjt@toerring.de (Jens Thoms Toerring)
Date2013-06-11 21:36 +0000
Message-ID<b1pjj9Fbe8oU1@mid.uni-berlin.de>
In reply to#3401
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web