10035 - Primary Arithmetic

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

Moderator: Board moderators

Post Reply
koniloni
New poster
Posts: 15
Joined: Thu Apr 18, 2002 5:10 am

10035

Post by koniloni »

What is wrong with my problem?I have gotten "wrong answer".This is my code:
#include<stdio.h>
int main()
{
unsigned long x,y,a,b,c,d;
while((scanf("%ld%ld",&x,&y)==2)&&(x!=0 && y!=0))
{
if(x<=999999999 &&y<=999999999)
{
d=0,c=0;
while(x!=0 && y!=0)
{
a=x%10;
b=y%10;
x=x/10;
y=y/10;
if(a+b+d>=10)
{
++c;
d=1;
}
}
if(c==0)
printf("No carry operation\n");
else if(c==1)
printf("1 carry operation\n");
else
printf("%d carry operations\n",c);
}
}
return 0;
}
/*" @END_OF_SOURCE_CODE"*/
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

I have obverse one minor mistake, just a MINOR one, but I donno if it lead to your WA.

Please check your outputs with the sample ones.
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

help me

Post by Shahid »

hi, i did this program, but getting WA. i think this is quite an easy problem...and my code is ok. can anyone help me? here is my code:


[c]

/*@BEGIN_OF_SOURCE_CODE*/



#include<stdio.h>


void main()
{
int count, ad = 0, ag = 0, carry, sum = 0;
unsigned long adnd, agnd, tmp;

for(;;)
{
scanf("%lu %lu", &adnd, &agnd);

if(!adnd && !agnd)
break;

count = 0;
carry = 0;

if(adnd < agnd)
{
tmp = adnd;
adnd = agnd;
agnd = tmp;
}

while(adnd)
{
ad = adnd % 10;
ag = agnd % 10;

sum = ad + ag + carry;

if(sum > 9)
{
count++;
carry = sum / 10;
}

adnd /= 10;
agnd /= 10;
}

if(!count)
printf("No carry operation.\n");

else if(count == 1)
printf("1 carry operation.\n");

else
printf("%d carry operations.\n", count);
}

}

/*@END_OF_SOURCE_CODE*/

[/c]
galois_godel
New poster
Posts: 17
Joined: Wed Jul 17, 2002 5:00 pm

10035 WR ? why?

Post by galois_godel »

It's a easy problem. But I can't get AC.
Who can tell me why?
program a10035;

{$APPTYPE CONSOLE}

uses
SysUtils;
var
a,b,i,c,temp:integer;
m,n:array[1..10]of integer;
lm,ln,l,carry:integer;
begin
{ TODO -oUser -cConsole Main : Insert code here }
{assign(input,'a10035.in.txt');
assign(output,'a10035.out.txt');
reset(input);
rewrite(output);}
while not eof(input) do
begin
readln(a,b);
if (a=0)and(b=0)then exit;
lm:=0;
while a<>0 do
begin
lm:=lm+1;
m[lm]:=a mod 10;
a:=a div 10;
end;
ln:=0;
while b<>0 do
begin
ln:=ln+1;
n[ln]:=b mod 10;
b:=b div 10;
end;
if ln<lm then l:=ln
else
l:=lm;
c:=0;
carry:=0;
for i:=1 to l do
begin
temp:=c+m+n;
if temp>9 then
carry:=carry+1;
c:=(m+n)div 10;
end;
if carry=0 then
writeln('No carry operation.')
else
writeln(carry,' carry operations.');
end;

{close(input);
close(output);}

end.
rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Post by rakeb »

if u get one carry operation the u have to print "1 carry operation." , not - "1 carry operationS." :lol: . i think this is ur mistake
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

thanx

Post by Shahid »

hi thanx....for ur clue...

i got accepted.... :)
Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

10035

Post by Eric »

It should be a easy program, however, I got wrong answer. What goes wrong?
Last edited by Eric on Sun Apr 20, 2003 11:56 am, edited 2 times in total.
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 »

i think there is an input with one of the number is 0, which your program does not handle...

Code: Select all

9 0
0 9
give it a try :wink:
Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

P 10035

Post by Red Scorpion »

You should notice that "each line of input contains two unsigned integer less than 10 digits".

You cannot use a long integer to sum that numbers, try to use string!
Astrakan
New poster
Posts: 24
Joined: Sun Nov 03, 2002 12:18 pm
Location: Sweden

Post by Astrakan »

Using long int works fine, since the sum of two 9-digit numbers is less than 2000000000 which fits in a long int, and in this case the sum is not even calculated.

There is another error error in the program.

Try the case

Code: Select all

9899 1
The answer should be 2, not 3.
TWEmp
New poster
Posts: 7
Joined: Tue Jan 21, 2003 4:52 am
Location: Hong Kong
Contact:

Post by TWEmp »

Astrakan wrote:Using long int works fine, since the sum of two 9-digit numbers is less than 2000000000 which fits in a long int, and in this case the sum is not even calculated.
So does that explains why my program gets a WA either?

int main() {
int a, b, c;
int i, count, carry;
do {
scanf("%d %d", &a, &b);
if (((a > 0 && b >= 0) || (a >= 0 && b > 0)) && (a < 1000000000 && b < 1000000000)) {
i = 1;
count = 0;
carry = 0;
while (a / i >= 1 || b / i >= 1) {
i *= 10;
c = (a % i) * 10 / i + (b % i) * 10 / i + carry;
if (c >= 10) count++;
carry = c / 10;
}
switch (count) {
case 0:
printf("No carry operation.\n");
break;
case 1:
printf("1 carry operation.\n");
break;
default:
printf("%d carry operations.\n", count);
}
}
} while (a != 0 || b != 0);
return 0;
}
Astrakan
New poster
Posts: 24
Joined: Sun Nov 03, 2002 12:18 pm
Location: Sweden

Post by Astrakan »

TWEmp, try this case, which your program doesn't handle correctly:

Code: Select all

999999999 999999999
The sum of two 9-digit numbers fits in a long int, but the product of a 9-digit number and 10 does not always fit.
thechaoshacker
New poster
Posts: 6
Joined: Wed Jan 22, 2003 9:19 pm
Contact:

10035

Post by thechaoshacker »

I am getting a WA for this one. Can't figure out where I went wrong. Could somebody help me please ?

[cpp]#include<iostream.h>
unsigned long long a,b;

int main()
{
int dig1,dig2,carry=0,sum,count=0;
while(1)
{
cin>>a>>b;
if(!a&&!b)
break;
count=0;
carry=0;
while(a||b)
{
dig1=a%10;
dig2=b%10;
sum=dig1+dig2+carry;
if(sum>=10)
{
carry=1;
count++;
}
else
carry=0;
a/=10;
b/=10;
}
if(0==count)
cout<<"No carry operation.\n";
else
{
if(1==count)
cout<<"1 carry operation.\n";
cout<<count<<" carry operations.\n";
}
}
return 0;
}

[/cpp]
thanx,
thechaoshacker
Astrakan
New poster
Posts: 24
Joined: Sun Nov 03, 2002 12:18 pm
Location: Sweden

Post by Astrakan »

Try a case which gives one carry operation, for example

Code: Select all

15 15
thechaoshacker
New poster
Posts: 6
Joined: Wed Jan 22, 2003 9:19 pm
Contact:

Stupid Mistake

Post by thechaoshacker »

I forgot the else condition for

if(1==count)
cout<<"1 carry operation.\n";
else
cout<<count<<" carry operations.\n";

oops :)
thechaoshacker }
Post Reply

Return to “Volume 100 (10000-10099)”