465 - Overflow

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

afonsocsc
New poster
Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon

Post by afonsocsc » Fri Jul 16, 2004 2:56 pm

Hi,
In the manpage of puts:
"puts() writes the string s and a trailing newline to stdout."
You're putting a newline at the end of each line of the input, so you should use either fputs or printf...

Hope it helps!

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by Guest » Fri Jul 16, 2004 5:09 pm

But in case of using printf, i would still have to put a newline after the string.. i think the prob is elsewhere.. :cry:
anyway, thank you for replying.

afonsocsc
New poster
Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon

Post by afonsocsc » Fri Jul 16, 2004 5:51 pm

gets() also gets the newline from input, so if you use puts() it will have two newlines, if you want to use puts(), you simply have to remove the '\n' of the string retrieved by gets().

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by Guest » Sat Jul 17, 2004 10:06 am

But my output format still looks correct. Perhaps you would like to compile and run it?

afonsocsc
New poster
Posts: 34
Joined: Mon Mar 24, 2003 1:15 am
Location: Portugal, Lisbon

Post by afonsocsc » Sat Jul 17, 2004 12:06 pm

Ok, sorry, I was confusing gets() with fgets(), gets() doesn't put the newline in the string...
But I found a bug in the code, try this input:
999999999999999999 * 1

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

Post by Guest » Sat Jul 17, 2004 8:21 pm

Thank you so much for pointing out my mistake, :D
At last I've got accepted, :D

Thank you again!

jackie
New poster
Posts: 47
Joined: Tue May 04, 2004 4:24 am

Post by jackie » Tue Aug 03, 2004 1:19 pm

After many WAs i change the algorithm. Use double instead of big number.
and i get AC.
Then i find the judge's input doesn't contain line like
1000000*10000

or

1000000 + -1
simply input

[cpp] while (scanf("%s%s%s", a, op, b) == 3)
{
printf("%s %s %s\n", a, op, b);
//decide how to output
}[/cpp]

hope it can help

good luck

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Tue Aug 03, 2004 7:13 pm

jackie wrote:After many WAs i change the algorithm. Use double instead of big number.
and i get AC.

hope it can help

good luck
It does help - I've just got AC with a 10-line program. I guess this post is resolved now. :D

shanto86
Experienced poster
Posts: 160
Joined: Wed Jul 30, 2003 8:10 pm

problem!!!

Post by shanto86 » Mon Aug 30, 2004 9:53 am

can any one plz hlp me with this code. i am getting WA.

#include<stdio.h>

void main()
{
long long n1,n2,result;
char expression[1000],op[3];
while(gets(expression))
{
sscanf(expression,"%lld%s%lld",&n1,op,&n2);
printf("%s\n",expression);
if(n1>2147483647)
printf("first number too big\n");
if(n2>2147483647)
printf("second number too big\n");
if(op[0]=='+')
result=n1+n2;
else
result=n1*n2;
if(result>2147483647)
printf("result too big\n");
}
}
Self judging is the best judging!

minskcity
Experienced poster
Posts: 199
Joined: Tue May 14, 2002 10:23 am
Location: Vancouver

Post by minskcity » Mon Aug 30, 2004 6:00 pm

Read previos topic - use double instead of long long... If you are given a very big number as an input, it might look like a small number when converted to long long and your code will fail.

Arm.Turbo
New poster
Posts: 21
Joined: Wed Aug 11, 2004 1:20 pm

Re: problem!!!

Post by Arm.Turbo » Wed Sep 08, 2004 2:33 pm

shanto86 wrote:can any one plz hlp me with this code. i am getting WA.

#include<stdio.h>

void main()
{
skip
}
You can't use long long. You have to scan data from input using strings.

hectorUCI
New poster
Posts: 4
Joined: Tue Mar 01, 2005 8:36 am

Problem 465

Post by hectorUCI » Fri Mar 04, 2005 7:52 am

I think this code works but .. i get WA. Could anybody check it, please?

Program ACM465;
var
I,D : LongInt;
p,pm,pp,pei,ped:integer;
S,SI,SD : String;
b:boolean;
Begin
while not eof do
begin
readln( S );
pm:=pos('+',S); pp:=pos('*',S); b:=FALSE;
p:=pm+pp;
if p=0 then continue;
SI:=copy(S,1,p-1);
while not (SI[1] in ['0'..'9']) do delete(SI,1,1);
while not (SI[length(SI)] in ['0'..'9']) do delete(SI,Length(SI),1);
val(SI,I,pei);
SD:=copy(S,p+1,length(S)-p);
while not (SD[1] in ['0'..'9']) do delete(SD,1,1);
while not (SD[length(SD)] in ['0'..'9']) do delete(SD,Length(SD),1);
val(SD,D,ped);
writeln(S);
if (I>32767) or (pei<>0) then
begin
writeln('first number too big');
b:=TRUE;
end;
if (D>32767) or (ped<>0) then
begin
writeln('second number too big');
b:=TRUE;
end;
if (pm<>0) and (I+D>32767) then b := TRUE;
if (pp<>0) and (I*D>32767) then b := TRUE;
if b then
writeln('result too big');
end;
End.

murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

465....Having problem in taking input

Post by murkho » Thu Mar 31, 2005 8:32 pm

I am having problem in taking input for problem 465...that is should i use
strtok() or sprintf() ?
if i use sprintf()
then why this line does not work?

sprintf(str,"%s %ch %s",&s1,&ch,& s2);
it can take first number in s1 , the + or * sign in ch
but not second number in s2.

if to use strtok how to use it?

any other possible ways?


please help me . I am waiting for your help.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Fri Apr 01, 2005 6:42 am

None of strtok()/sprintf() are used for taking input.At least as far as I know.
Maybe you are doing a fgets() and then this strtok() /sprintf stuff ...but I am left to guess.

Code: Select all

sprintf(str,"%s %ch %s",&s1,&ch,& s2); 
Can you tell me the type specifiers for s1, s2 & ch?I am guessing once again,
s1 and s2 are of type char */char [] - in which case your code is wrong.
If s1, s2 are integers - %s has to be changed. So either way this snippet is plain wrong.

Code: Select all

int main()
{
    char a[256], b[256], op[2];
     ...
    while ( 3 == scanf("%s %s %s", a, op, b) )
    {
          /*....a lot of stuff that I wont tell you ;)*/
    }
...
}
is how I get input for this problem.

HTH
Regards,
Suman.

User avatar
Rocky
Experienced poster
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am
Contact:

465:Pls.....Help Me

Post by Rocky » Wed Jun 01, 2005 9:11 am

Can Any Body Help With Any Tricks Or Special IO Of This Problem
I Get WA All The Time.
Plsss Help Me.

THANK'S IN ADVANCE
Rocky

Post Reply

Return to “Volume 4 (400-499)”