Page 10 of 22
Posted: Sat Mar 24, 2007 10:54 am
by shamim
You are not considering carry forwarded.
try the case:
input
999
989
output
3
Posted: Sat Mar 24, 2007 8:35 pm
by Rushow
You are not considering carry forwarded.
try the case:
input
999
989
output
3
I have tried this case.
And I have found output 3.
So, again question what's the problem? I have changed my code as below. But no result:
Code: Select all
/* p10035 */
/* Primary Arithmetic */
#include<stdio.h>
#include<math.h>
void main()
{
unsigned long int n1,n2,r1,r2;
int s,sum,num;
while(scanf("%lu%lu",&n1,&n2)!=EOF)
{
if(n1==0&&n2==0)
break;
s=0;num=0;
while((n1>0)||(n2>0))
{
r1=n1%10;
n1=n1/10;
r2=n2%10;
n2=n2/10;
sum=r1+r2+num;
if(sum>9)
{
s++;
if(sum>9)
num=sum-9;
}
}
if(s==0)
printf("No carry operation.\n");
else if(s==1)
printf("1 carry operation.\n");
else
printf("%d carry operations.\n",s);
}
}
Posted: Sat Mar 24, 2007 10:12 pm
by abdullah<cse du>
Hi,
Just try this input.
5210357 1245874
Correct output is : 3 carry operations.
But your output is : 4 carry operations.
You have done some mistakes in carry operation.
ABDULLAH.
10035~Help Me!!!
Posted: Sat Apr 07, 2007 3:19 pm
by ranacse05
Any body plz tell me wheer is the error.
I don't find any error and i tryed all kind of I/O,but i got WA!
*************Code Removed***********
Posted: Sat Apr 07, 2007 11:26 pm
by Jan
Try input '99 1'.
Posted: Sun Apr 08, 2007 8:47 am
by ranacse05
I try it and the answer is 2 carry operations.Its ok,then where is the problem?
Posted: Sun Apr 08, 2007 8:58 am
by ranacse05
Sorry that out put was ok in Turbo C but VC++ out put was wrong.
Pls cheack this one
********Code Removed***********
Posted: Sun Apr 08, 2007 4:18 pm
by Jan
Check your code again. What is 'n'? And why are you using it? And 'long unsigned int' doesn't make any sense. To post codes you can use the 'code' option from the editor. And you can remove your unnecessary codes by editing posts.
Posted: Sun Apr 08, 2007 10:33 pm
by ranacse05
Thanks Jan,at last i got AC,Can i Get ur email address?I'm in 3rd semister in RUET,(CSE).Some time i'll ask u for help bcoz this board take too much time.

Posted: Sun Apr 15, 2007 3:39 pm
by mahfuz05
thanks spykaj i got ac.
.......******* CODE REMOVED******...........
Posted: Sun Apr 15, 2007 5:41 pm
by Spykaj
If you paste your code, use:
You forget dots, it should be:
Code: Select all
if(carry==0) printf("No carry operation.\n");
instead of
Code: Select all
if(carry==0) printf("No carry operation\n");
Posted: Sun Apr 15, 2007 7:54 pm
by LZ
I've been trying so many times but i still got WA. Please help me to find the mistakes.
#include<cstdio>
#include<cstdlib>
#include<cstring>
int main()
{
char a[15], b[15];
unsigned long int c=1,d=1;
int len, p, jml=0,ctr;
while(1)
{
ctr=0;
scanf("%s %s",&a ,&b);
sscanf(a,"%ld",&c);
sscanf(b,"%ld",&d);
if(c==0&&d==0)break;
if(strlen(a)>strlen(b))
{
len=strlen(a);
p=strlen(b)-1;
for(int i=len-1;i>=0;i--)
{
if(p>=0)
{
jml = jml + a[i]+b[p]-96;
p--;
}
else jml = jml + a[i]-48;
if(jml>9)
{
ctr++;
jml = jml/10;
}
else jml=0;
}
}
else
{
len=strlen(b)-1;
p=strlen(a)-1;
for(int i=len;i>=0;i--)
{
if(p>=0)
{
jml = jml + b[i]+a[p]-96;
p--;
}
else jml = jml + b[i]-48;
if(jml>9)
{
ctr++;
jml = jml/10;
}
else jml=0;
}
}
if(ctr==0)printf("No carry operation.\n");
else if(ctr==1)printf("1 carry operation.\n");
else printf("%d carry operations.\n", ctr);
}
return 0;
}
Posted: Sun Apr 15, 2007 8:07 pm
by ranacse05
LZ why you take char when you can use long long int???
Posted: Sun Apr 15, 2007 8:17 pm
by LZ
[quote="ranacse05"]LZ why you take char when you can use long long int???[/quote]
I'm just curious. Actually, i made another program using long but i still got WA. Here's the codes. Please check it if you're not busy. thx alot.
#include<cstdio>
int main()
{
long int a, b, input1, input2;
int ctr, jml=0, i;
while(a!=0||b!=0)
{
ctr=0;
scanf("%ld %ld", &a, &b);
input1=a;
input2=b;
for(i=0;i<10;i++)
{
jml = jml + input1%10 + input2%10;
input1/=10;
input2/=10;
if(jml>9)
{
ctr++;
jml = jml/10;
}
else jml=0;
if(input1==0&&input2==0)break;
}
if(ctr==0)printf("No carry operation.\n");
else if(ctr==1)printf("1 carry operation.\n");
else printf("%d carry operations.\n", ctr);
}
return 0;
}
Posted: Sun Apr 15, 2007 8:48 pm
by Spykaj