713 - Adding Reversed Numbers

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

Moderator: Board moderators

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Thu Jan 29, 2004 12:17 pm

I got AC.

here are some sample inputs and outputs. Hope these will help you. :-)

Input:

100 200
999999999999999 1
00010 9
1 9
1 9
5 5
456 654
0 0



Output:

3
1
9001
1
1
1
111
0

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Thu Jan 29, 2004 1:29 pm

thanks,but none of them can proove my code is wrong.it's so weird
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Thu Jan 29, 2004 2:24 pm

May be there are numbers like 10^1000 or so. I used two strings of length 1000 to store the input numbers. I can't code in JAVA, so could not understand wheather you took care of that or not. Did you :-?

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

713

Post by WR » Thu Jan 29, 2004 2:58 pm

I think there's been a rejudgement yesterday, because my program
had already been accepted and now it's missing from the list of
accepted programs. Submitting it again led to a WA, so I guess
I was just lucky too.

sjn
Learning poster
Posts: 73
Joined: Mon Apr 08, 2002 8:22 am
Contact:

Post by sjn » Thu Jan 29, 2004 5:58 pm

10^500 is enough :wink:

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Thu Jan 29, 2004 7:16 pm

Dear Sohel, I think you should use string instead of long.

User avatar
Dreamer#1
Learning poster
Posts: 77
Joined: Tue Oct 07, 2003 10:07 pm

Post by Dreamer#1 » Fri Jan 30, 2004 9:33 am

I know its really stupid but the new input set contains numbers as big as 10^100 or larger, so, you must use string arithmetic to get through.

Hope it helps. :)

The most horrible thing is that the problem description never had any range for input so most of the old AC solutions didn't take that into account and getting WA after rejudgement. :(
Not all of our dreams can be made to come true. But still we live with the hope to turn them into reality someday.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Fri Jan 30, 2004 9:42 am

Thanks so much,i'll try to solve it
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg

Post by pavelph » Fri Jan 30, 2004 7:35 pm

No, I think that Sohel has problem in other way ( in my program I used array [1 .. 500] and get AC).
Sohel, maybe your program write zeros at the begin of line or at the end of it.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Fri Jan 30, 2004 7:51 pm

hey,I think the number must be giant.

[cpp]
#include "iostream.h"
#include "string.h"
int main(int argc, char* argv[])
{
unsigned int ifAdd,loop,i;
char a[10000],b[10000],temp[10000];
while(cin>>a>>b)
{
if(a[0]==b[0] && b[0]=='0' && b[1]=='\0') cout<<'0'<<endl;
else
{
for(loop=0;loop<strlen(a);loop++)
{
while(a[loop]=='0')
for(i=loop;i<strlen(a);i++)
a=a[i+1];
break;
}
for(loop=0;loop<strlen(b);loop++)
{
while(b[loop]=='0')
for(i=loop;i<strlen(b);i++)
b=b[i+1];
break;
}


ifAdd=0;
if(strlen(a)<strlen(b))
{
strcpy(temp,a);
strcpy(a,b);
strcpy(b,temp);
}
strcpy(temp,a);

for(loop=0;loop<strlen(b);loop++)
{
temp[loop]=(a[loop]+b[loop]+ifAdd-96)%10+48;
if(a[loop]+b[loop]-96>9) ifAdd=1;
else ifAdd=0;
}
if(ifAdd!=0)
{
while(ifAdd!=0 && loop<strlen(a))
{
temp[loop]=(a[loop]+ifAdd-48)%10+48;

if(temp[loop]+1<10) ifAdd=0;
loop++;
}
if(ifAdd!=0 && loop==strlen(a))
{
temp[loop]=49;
temp[loop+1]='\0';
}
}
for(loop=strlen(temp)-1;loop>0;loop--)
{
if(temp[loop]=='0')
{
temp[loop]='\0';
}
else break;
}
for(loop=0;loop<strlen(temp);loop++)
if(temp[loop]!='0') break;
for(;loop<strlen(temp);loop++) cout<<temp[loop];
cout<<endl;
}
}
return 0;
}
[/cpp]
at first i defined my array as large as 100;and got RE,then i changed it into 10000,then got WA.who can tell me where's the problem?:(
Last edited by Morning on Fri Jan 30, 2004 9:31 pm, edited 1 time in total.
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Fri Jan 30, 2004 7:52 pm

i still got WA:(
[cpp]
#include "iostream.h"
#include "string.h"
int main(int argc, char* argv[])
{
unsigned int ifAdd,loop,i;
char a[10000],b[10000],temp[10000];
while(cin>>a>>b)
{
if(a[0]==b[0] && b[0]=='0' && b[1]=='\0') cout<<'0'<<endl;
else
{
for(loop=0;loop<strlen(a);loop++)
{
while(a[loop]=='0')
for(i=loop;i<strlen(a);i++)
a=a[i+1];
}
for(loop=0;loop<strlen(b);loop++)
{
while(b[loop]=='0')
for(i=loop;i<strlen(b);i++)
b=b[i+1];
}


ifAdd=0;
if(strlen(a)<strlen(b))
{
strcpy(temp,a);
strcpy(a,b);
strcpy(b,temp);
}
strcpy(temp,a);

for(loop=0;loop<strlen(b);loop++)
{
temp[loop]=(a[loop]+b[loop]+ifAdd-96)%10+48;
if(a[loop]+b[loop]-96>9) ifAdd=1;
else ifAdd=0;
}
if(ifAdd!=0)
{
while(ifAdd!=0 && loop<strlen(a))
{
temp[loop]=(a[loop]+ifAdd-48)%10+48;

if(temp[loop]+1<10) ifAdd=0;
loop++;
}
if(ifAdd!=0 && loop==strlen(a))
{
temp[loop]=49;
temp[loop+1]='\0';
}
}
for(loop=strlen(temp)-1;loop>0;loop--)
{
if(temp[loop]=='0')
{
temp[loop]='\0';
}
else break;
}
for(loop=0;loop<strlen(temp);loop++)
if(temp[loop]!='0') break;
for(;loop<strlen(temp);loop++) cout<<temp[loop];
cout<<endl;
}
}
return 0;
}
[/cpp]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Post by razibcse » Fri Jan 30, 2004 8:29 pm

To Raiyan Kamal:
I got AC.

here are some sample inputs and outputs. Hope these will help you.

Input:

100 200

999999999999999 1

00010 9

1 9

1 9

5 5

456 654

0 0

Output:

3

1

9001

1

1

1

111

0
But the preoblem statement said,
Also note that the reversed number never has any trailing zeros.
So when you use 00010 and 9 as inputs, 00010 becomes 1 and the result becomes 1 rather than 9001.

And both the input numbers are positive,so there will be no zero as input.

R@zib
Wisdom is know what to do next; virtue is doing it.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Fri Jan 30, 2004 8:32 pm

first of all,thanks for ur help indeed.
i think there will be 0 0 because of ur sample input.:)
and i got the right answer to the input 0010 9,it's 1 right?
so there must be any other problem:(
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

rjhadley
Learning poster
Posts: 73
Joined: Mon Oct 14, 2002 7:15 am
Location: United States

Post by rjhadley » Sat Jan 31, 2004 4:27 am

I don't think
0010 9
is valid input. But for
10 9
the output should be
1
.

Also, it looks like the new input set might have whitespace problems since there are only 39 AC, but 802 PE...

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Sat Jan 31, 2004 6:48 am

In the problem destcription it has been said that there will be no trailing zero(s). So I removed all the trailing zero(s). It has been also said that all the leading zeros are omitted while reversing. But the judge usually does strange things, specially after rejudgement. So I thaught there might be input with leading zero(s). That's why I care about that --- 00010 9 --- case. I know, it is not a valid input. But, there is no line in the problem description saying that there will be no invalid input.

By the way, my AC code gives 9001 as out put for that case.

Thanks to everyone.

Post Reply

Return to “Volume 7 (700-799)”