## 10035 - Primary Arithmetic

Moderator: Board moderators

maruf
New poster
Posts: 17
Joined: Sat May 24, 2008 6:00 pm

### Re: 10035 - Primary Arithmetic

remove after acc
Last edited by maruf on Sun Jun 15, 2008 4:57 pm, edited 1 time in total.
lives for eternity......

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:

### Re: 10035 - Primary Arithmetic

Check the sample very carefully.
Ami ekhono shopno dekhi...
HomePage

jesun
New poster
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

### Re: 10035 - Primary Arithmetic WA why?

I have tried all sample inputs in this thread and got correct answers.However the on-line judge is giving WA.I can't find any bug.Could someone please help me to track down the bug?
here is my code:

Code: Select all

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#include <stdlib.h>

void carrycount(int m,int n);

int main()
{
int m,n;
string s1,s2;
int p=0;
for(;;)
{
cin>>s1>>s2;
m=atoi(s1.data());
n=atoi(s2.data());
if (!m && !n)
{
break;
}
if(p++)
cout<<endl;

if(!m)
{
if(n)
{
cout<<"No carry operation.";
continue;
}
}
if(m)
{
if(!n)
{
cout<<"No carry operation.";
continue;
}
}
carrycount(m,n);
}
return 0;
}

void carrycount(int m,int n)
{
int len1,len2;
int i;
int nn=0;
int len,lenn;
int count=0;
int carry=0;
int temp;
int j=0;
len1=int(log10(m)+1);
len2=int(log10(n)+1);
if(len2>len1)
{
temp=n;n=m;m=temp;
temp=len2;len2=len1;len1=temp;
}
int* digits1=new int[len1];
int* digits2=new int[len2];
int num;
nn=m;
for(i=len1-1;i>=0;i--)
{
digits1[i]=nn%10;
nn=nn/10;
}

nn=n;
for(i=len2-1;i>=0;i--)
{
digits2[i]=nn%10;
nn=nn/10;
}
if(len1==len2)
{
for(i=len1-1;i>=0;i--)
{
if(digits1[i]+digits2[i]+carry>9)
{
count++;
carry=1;
}
}
if(count==0)
{
cout<<"No carry operation.";
}
else if(count==1)
{
cout<<"1 carry operation.";
}
else
{
cout<<count<<" carry operations.";
}
//return;
}

else
{
//len=((len1>=len2)?len1:len2);
//no
//lenn=((len1>=len2)?len2:len1);
for(i=len1-1,j=len2-1;j>=0;i--,j--)
{
if(digits1[i]+digits2[j]+carry>9)
{
count++;
carry=1;
}
else
{
carry=0;
}
}
if(carry)
{
for(;i>=0;i--)
{
if(digits1[i]+carry>9)
{
count++;
carry=1;
}
else
{
break;
}
}
}
if(count==0)
{
cout<<"No carry operation.";
}
else if(count==1)
{
cout<<"1 carry operation.";
}
else
{
cout<<count<<" carry operations.";
}
//return;
}
}

``````

shorojini
New poster
Posts: 8
Joined: Sat Aug 16, 2008 8:14 am

### Re: 10035 - Primary Arithmetic

all time i submit this code the verdict shows-"run time error"
i have compiled it for many times but cant get the fault.pls help

#include<iostream>
using namespace std;
int main()
{
int k,m,n,i=0,j=0,t=0,l,x;
int f,s[j],p[t],q[t];
long long a,b;
while(cin>>a>>b)
{
if(a!=0&&b!=0)
{
p[t]=a;
q[t]=b;
t++;
}
else
{
for(x=0;x<t;x++)
{
while(p[x]!=0)
{
f=p[x]%10;
p[x]=p[x]/10;
i++;
}
while(q[x]!=0)
{
s[j]=q[x]%10;
q[x]=q[x]/10;
j++;
}
if(i>j)
{
n=i;
for(k=j;k<i;k++)
{
s[k]=0;
}
}
else if(j>i)
{
n=j;
for(k=i;k<j;k++)
{
f[k]=0;
}
}
else if (i==j)
n=i;

j=0;
l=0;
for(m=0;m<n;m++)
{
k=f[m]+s[m]+l;
if(k>9)
{j++;
l=1;}
else
l=0;
}

if(j==0)
cout<<"No carry operation."<<"\n";
else if(j==1)
cout<<"1 carry operation."<<"\n";
else if (j>1)
cout<<j<<"carry operations."<<"\n";

i=0;
j=0;
}
t=0;
}

}

return 0;
}

debugger
New poster
Posts: 8
Joined: Tue Jul 29, 2008 6:24 pm

### Re: 10035 - Primary Arithmetic

can any body give some critical input case?

debugger
New poster
Posts: 8
Joined: Tue Jul 29, 2008 6:24 pm

### Re: 10035 - Primary Arithmetic

Can any body say what is wrong with this code. I get wrong answer but i tested my code for many critical input.Please help me.
following is my code:

Code: Select all

``````#include<stdio.h>

int main(){

int a,b,a1[10],b1[10],carry;
int i,j,k,sum,max;
char ch;
for(;;){
scanf("%d %d",&a,&b);
if(a==0 && b==0)
break;
else{
carry=0;
sum=0;
i=0;j=0;
for(k=0;k<10;k++)
a1[k]=b1[k]=0;

for(;;){
if(a==0)
break;
else{
a1[i]=a%10;
a=a/10;
i++;

}
}//convert into int array
for(;;){
if(b==0)
break;
else{
b1[j]=b%10;
b=b/10;
j++;
}
}//convert into int array
i=i-1;
j=j-1;
if(i>j)
{max=i;for(j=j+1;j<=max;j++)b1[j]=0;}
else if(j>i)
{max=j;for(i=i+1;i<=max;i++)a1[i]=0;}
else max=i=j;
for(k=0;k<=max;k++)
{sum=sum+((a1[k]+b1[k]+carry)/10);
carry=(a1[k]+b1[k]+carry)/10;
}
if(sum==0)
printf("No carry operation.\n");
else if(sum==1)
printf("%d carry operation.\n",sum);
else printf("%d carry operations.\n",sum);
}
}
return 0;
}

``````

shorojini
New poster
Posts: 8
Joined: Sat Aug 16, 2008 8:14 am

### Re: 10035 - Primary Arithmetic

i alltime get runtime error.pls help.i want to know why?

#include<iostream>
using namespace std;
int main()
{
int k,m,n,i=0,j=0,t=0,l,x;
int f,s[j],p[t],q[t];
long long a,b;
while(cin>>a>>b)
{
if(a!=0&&b!=0)
{
p[t]=a;
q[t]=b;
t++;
}
else
{
for(x=0;x<t;x++)
{
while(p[x]!=0)
{
f=p[x]%10;
p[x]=p[x]/10;
i++;
}
while(q[x]!=0)
{
s[j]=q[x]%10;
q[x]=q[x]/10;
j++;
}
if(i>j)
{
n=i;
for(k=j;k<i;k++)
{
s[k]=0;
}
}
else if(j>i)
{
n=j;
for(k=i;k<j;k++)
{
f[k]=0;
}
}
else if (i==j)
n=i;

j=0;
l=0;
for(m=0;m<n;m++)
{
k=f[m]+s[m]+l;
if(k>9)
{j++;
l=1;}
else
l=0;
}

if(j==0)
cout<<"No carry operation."<<"\n";
else if(j==1)
cout<<"1 carry operation."<<"\n";
else if (j>1)
cout<<j<<"carry operations."<<"\n";

i=0;
j=0;
}
t=0;
}

}

return 0;
}

abhishekbose87
New poster
Posts: 4
Joined: Sun Dec 21, 2008 5:12 pm

### Re: 10035 - Primary Arithmetic

I cannot understand why I am getting WA ... Plz help me

Code: Select all

``````#include<stdio.h>
long long int a ,b;
void convert(int *num,int *digits,long long int n)
{
int i;
*digits=0;
while(n>0)
{
num[*digits]=(int)(n%10);
n=n/10;
(*digits)++;
}
for(i=*digits;i<11;i++)
num[i]=0;
}
void find()
{
int count=0,digit1,digit2,digit;
int num1[11],num2[11];
convert(num1,&digit1,a);
convert(num2,&digit2,b);
digit=digit1>digit2?digit1:digit2;
int carry=0,i,sum;
for(i=0;i<digit;i++)
{
sum=num1[i]+num2[i]+carry;
if(sum>9)
{
carry=1;
count++;
}
else carry=0;
}
if(count==0)
printf("No carry operation.");
else if(count ==1)
printf("1 carry operation.");
else printf("%d carry operations.",count);
}
int main()
{
int count=0;
while(1)
{
scanf("%lld",&a);
scanf("%lld",&b);
if(a==0 && b==0)
return 0;
if(count>0)
printf("\n");
find();
count++;
}
return 0;
}``````

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### 10035 - WA,WHY?

can anyone help???
whats wrong!!!!!!!.why wrong ans????

Code: Select all

``````
int  main()

{
unsigned long a,b;
unsigned carry;
unsigned long ncarry,x;

while(scanf("%lu %lu",&a,&b)==2)

{

if(a==0 && b==0)
break;
carry=0;
ncarry=0;

while(a>0 || b>0)

{

x=a%10+b%10+carry;

if(x>9){
ncarry=ncarry+1;
carry=1;
}

a=(int)a/10;
b=(int)b/10;

}

if(ncarry==1)

printf("1 carry operation.\n");

else  if(ncarry>1)
printf("%lu carry operations.\n",ncarry);

else if(ncarry==0)

printf("No carry operation.\n");

}

return 0;

}
``````
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm

### Re: 10035 - Primary Arithmetic

When your x is greater than 9, you set carry to 1. But if x<=9 the carry should be set to 0. You forgot that.
Good luck.
May be tomorrow is a better day............

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### 10035 - Primary Arithmetic

plz someone help me.
i have tried all the way failed to get AC.
plz help
my code-

Code: Select all

``````code removed
``````
Last edited by sazzadcsedu on Sun Dec 28, 2008 1:01 am, edited 1 time in total.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 10035 - Primary Arithmetic

sorry sorry

thanx a lot articuno.
i got accepted.
plz help me in problem 10924(plz see in board 10924).
plz someone.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

toru
New poster
Posts: 17
Joined: Tue Dec 30, 2008 10:38 am

### 10035 - Primary Arithmetic

Hi !, i m new here, plzz any one help me, i tried all the input/output available here , it works correct, but when i m submitting in judge, it gives WA????......... i post my code beneath.. thankz in advance....

#include<stdio.h>
#include<string.h>
#define max 10000
#include<stdlib.h>

int Mystrrev(char *Str)
{
char Temp[10000]={'\0'};
int l=strlen(Str),i;
for(i=0;i<l;i++)
{
Temp=Str[l-i-1];
}
Temp='\0';
strcpy(Str,Temp);
return 0;
}

int main()
{
char str_1[max], str_2[max], result[max];
long i, x, len_1, len_2, diff,carry, count=0;
long in1, in2;

while(scanf("%s %s", str_1, str_2)!=EOF)
{
in1=atoi(str_1);
in2=atoi(str_2);
if(in1==0 && in2==0)
{
break;
}
else
{
count=0;
len_1=strlen(str_1);
len_2=strlen(str_2);

if(len_1>len_2)
{
x = len_1;
diff = len_1-len_2;
Mystrrev(str_1);
Mystrrev(str_2);
for(i=0;i<diff;i++)
str_2[len_2+i]='0';
}
else
{
x = len_2;
diff = len_2-len_1;
Mystrrev(str_2);
Mystrrev(str_1);
for(i=0;i<diff;i++)
str_1[len_1+i]='0';
}

carry=0;

for(i=0;i<x;i++)
{
if(carry)
count++; //counting
carry = carry+str_1-48+str_2-48;
result = carry%10+48;
carry = carry/10;
}

while(carry!=0)
{
if(carry)
count++;
result[x] = carry%10+48;
carry = carry/10;
x++;
}

result[x]='\0';

if(count==0)
printf("No carry operation.\n", count);
else if(count==1)
printf("1 carry operation.\n");
else
printf("%lld carry operations.\n", count);
}
}
return 0;
}

Articuno
Learning poster
Posts: 78
Joined: Sun Nov 30, 2008 5:00 pm

### Re: 10035 - Primary Arithmetic

Well there are a few lines you should recheck:
The format specifier for long data type is %ld , not %lld.
And you should also change this line:

Code: Select all

``while(scanf("%s %s", str_1, str_2)!=EOF)``
into this:

Code: Select all

``while(scanf("%s %s", &str_1, &str_2)==2)``
And what is count doing here?

Code: Select all

``printf("No carry operation.\n", count);``
Wish you good luck
May be tomorrow is a better day............

toru
New poster
Posts: 17
Joined: Tue Dec 30, 2008 10:38 am

### Re: 10035 - Primary Arithmetic

Thanks to UUUUUUUU "ARTICUNO"............... For the Help,changed little & it worked
: