10035 - Primary Arithmetic
Moderator: Board moderators
I have tried this case.You are not considering carry forwarded.
try the case:
input
999
989
output
3
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);
}
}
-
- New poster
- Posts: 39
- Joined: Mon Dec 04, 2006 2:18 pm
- Location: Bangladesh(CSE DU)
- Contact:
10035~Help Me!!!
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***********
I don't find any error and i tryed all kind of I/O,but i got WA!
*************Code Removed***********
Last edited by ranacse05 on Sun Apr 08, 2007 10:42 pm, edited 1 time in total.
Sorry that out put was ok in Turbo C but VC++ out put was wrong.
Pls cheack this one
********Code Removed***********
Pls cheack this one
********Code Removed***********
Last edited by ranacse05 on Sun Apr 08, 2007 10:36 pm, edited 1 time in total.
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.
Ami ekhono shopno dekhi...
HomePage
HomePage
If you paste your code, use:
You forget dots, it should be:
instead of
Code: Select all
Code: Select all
if(carry==0) printf("No carry operation.\n");
Code: Select all
if(carry==0) printf("No carry operation\n");
Last edited by Spykaj on Mon Apr 16, 2007 8:16 pm, edited 1 time in total.
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;
}
#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;
}
[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;
}
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;
}