10035  Primary Arithmetic
Moderator: Board moderators
10035
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"*/
#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"*/

 Learning poster
 Posts: 68
 Joined: Fri Oct 26, 2001 2:00 am
 Location: Dhaka, Bangladesh
 Contact:
help me
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]
[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]

 New poster
 Posts: 17
 Joined: Wed Jul 17, 2002 5:00 pm
10035 WR ? why?
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.
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.
i think there is an input with one of the number is 0, which your program does not handle...
give it a try
Code: Select all
9 0
0 9

 Experienced poster
 Posts: 192
 Joined: Sat Nov 30, 2002 5:14 am
P 10035
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!
You cannot use a long integer to sum that numbers, try to use string!
Using long int works fine, since the sum of two 9digit 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
The answer should be 2, not 3.
There is another error error in the program.
Try the case
Code: Select all
9899 1
So does that explains why my program gets a WA either?Astrakan wrote:Using long int works fine, since the sum of two 9digit numbers is less than 2000000000 which fits in a long int, and in this case the sum is not even calculated.
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;
}
TWEmp, try this case, which your program doesn't handle correctly:
The sum of two 9digit numbers fits in a long int, but the product of a 9digit number and 10 does not always fit.
Code: Select all
999999999 999999999

 New poster
 Posts: 6
 Joined: Wed Jan 22, 2003 9:19 pm
 Contact:
10035
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(ab)
{
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
[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(ab)
{
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
Try a case which gives one carry operation, for example
Code: Select all
15 15

 New poster
 Posts: 6
 Joined: Wed Jan 22, 2003 9:19 pm
 Contact:
Stupid Mistake
I forgot the else condition for
if(1==count)
cout<<"1 carry operation.\n";
else
cout<<count<<" carry operations.\n";
oops
thechaoshacker }
if(1==count)
cout<<"1 carry operation.\n";
else
cout<<count<<" carry operations.\n";
oops
thechaoshacker }