## 10035 - Primary Arithmetic

Moderator: Board moderators

rahat khan
New poster
Posts: 10
Joined: Mon May 23, 2011 1:12 pm

### Re: 10035 - primary arithmetic

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d,i,j,x,y,m;
while(1)
{
m=0;
scanf("%d %d",&a,&b);
if(a==0&&b==0)
break;
x=log10(a)+1;
y=log10(b)+1;
for(i=0;i<x;i++)
{
c=a%10;
a=a/10;
}
for(j=0;j<y;j++)
{
d[j]=b%10;
b=b/10;
}
if(x<=y)
{
for(i=0;i<x;i++)
{
if(c+d>9)
{
m=m+1;
}
}
}
if(x>y)
{
for(j=0;j<y;j++)
{
if(c[j]+d[j]>9)
{
m=m+1;
}
}
}
if(m==0)
printf("No carry operation.\n");
else if(m==1)
printf("%d carry operation.\n",m);
else
printf("%d carry operations.\n",m);
}
return 0;
}

SS_HASAN
New poster
Posts: 3
Joined: Mon Jun 13, 2011 6:38 pm

### Re: 10035 - Primary Arithmetic

#include<stdio.h>

int main()
{
long long m, n;
int sum, count, carry;

while(scanf("%I64d %I64d",&m,&n))
{
if(!m && !n) break;

count = 0;
sum = 0;
carry = 0;

for( ; m != 0 || n != 0; m /= 10, n /= 10)
{
sum = (m % 10) + (n % 10) + carry;

carry = 0;

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

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

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

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

ravingeek
New poster
Posts: 1
Joined: Sun Jul 03, 2011 2:47 am

### Re: 10035 - Primary Arithmetic

i can't find why the system refuse my code
i can't find where is the mistake

here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
void sum(char x[SIZE],long *sum) {
int i;
for(i=0; i<SIZE; i++)
*sum+=x;
}
int main() {
char x[SIZE], y[SIZE],test[SIZE];
long z[SIZE],i,carry,sum0,sum1;
memset(test,'0', sizeof(char)*SIZE);
while(1) {
memset(x,'0', sizeof(char)*SIZE);
memset(y,'0', sizeof(char)*SIZE);
memset(z, 0, sizeof(long)*SIZE);
carry=0,sum0=0,sum1=0;
memset(z, 0, sizeof(int)*SIZE);
scanf("%s %s",x,y);
sum(x,&sum0);
sum(y,&sum1);
if(sum0==432 && sum1==432)
break;
for(i=0; i<SIZE; i++) {
z=(x+y)-96;
if (z>=SIZE)
carry++;
}
if(x=='-'||y=='-')
carry=0;
memset(z,0,SIZE);
if(carry==0)
printf("No carry operation.\n");
else if(carry==1)
printf("1 carry operation.\n");
else
printf("%ld carry operations.\n",carry);
carry=0;
memset(z, 0, sizeof(int)*SIZE);
}
return 0;
}

New poster
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

### Re: 10035 - Primary Arithmetic

ravingeek wrote:i can't find why the system refuse my code
What do you mean by refuse ? Can you please tell the exact verdict?
ravingeek wrote:i can't find where is the mistake
Your code seems to compile correctly but gives incorrect output. Did you run your program on the inputs given in the previous posts?

Code: Select all

``````for(i=0; i<SIZE; i++) {
z[i]=(x[i]+y[i])-96;
if (z[i]>=SIZE) carry++;
}
``````
In ASCII 96 stands for E. So what are you trying to do actually? Remember, if adding two digits is < 10, you may have a carry which makes it 10. Hope it helps.

New poster
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

### Re: 10035 - Primary Arithmetic

UVA do not give any verdict for the code below. It only says,
Your submission with number ******* for the problem 10035 - Primary Arithmetic has failed with verdict .
Verdict what? Where is the problem?
Code accepted and removed. I feel stupid. The problem does explicitly specify less than 10 digits. While it certainly has input cases > 10 digits. Since, I've just made my arrays length 40 and it got accepted. This is unexpected. Can anyone report this to the right authority?

New poster
Posts: 16
Joined: Thu Apr 28, 2011 10:48 pm

### Re: 10035 - Primary Arithmetic

for anyone keep getting WA
just take care of the 'S' in case the output is greater than or equal to 1
and try this case:
Input:

Code: Select all

``999743 859``
Output:

Code: Select all

``````6 carry operations.
``````

rahat khan
New poster
Posts: 10
Joined: Mon May 23, 2011 1:12 pm

### Re: 10035 - Primary Arithmetic

#include<stdio.h>
int main()
{
long long a,b,c,m,n,x,y,i,e;
while(scanf("%lld %lld",&x,&y))
{
if(x==0&&y==0)
break;
c=0;
if(a<b)
{
a=y;
b=x;
}
else
{
a=x;
b=y;
}
e=0;
for(i=1;;i++)
{
m=a%10;
m=m+e;
a=a/10;
n=b%10;
b=b/10;
if((m+n)>9)
{
c++;
e++;
}
else
e=0;
if(a==0)
break;
}
if(c==0)
printf("No carry operation.\n");
else
printf("%lld carry operations.\n",c);
}
return 0;
}

expecto_petronum
New poster
Posts: 2
Joined: Mon Oct 31, 2011 10:55 am

### UVA 10035

I am getting wrong answer. I can't found any problems. Can any one help me?

Code: Select all

``````#include<iostream>
using namespace std;

int main()
{
long long unsigned num1,num2,rem1,rem2,q1,q2;
int carry,p_carry;
//scanf("%u%u",&num1,&num2);
cin>>num1>>num2;
while(num1!=0 && num2!=0)
{
q1=num1;
q2=num2;
carry=0;
p_carry=0;

while(q1>0 || q2>0)
{
rem1=q1%10;
rem2=q2%10;
q1=q1/10;
q2=q2/10;

if(p_carry==1)
{
p_carry=0;
if(rem1+rem2>8)
{
carry++;
p_carry=1;
}
}
else
{
if(rem1+rem2>9)
{
carry++;
p_carry=1;
}
}

}
if(carry==0)
cout<<"No carry operation.\n";

if(carry>1)
cout<<carry<< " carry operations.\n";
//printf("%d carry operations.\n",carry);
if(carry==1)
cout<<"1 carry operation.\n";

//scanf("%d%d",&num1,&num2);
cin>>num1>>num2;

}
return 0;
}``````

artless
New poster
Posts: 7
Joined: Tue Nov 22, 2011 2:01 pm

### why WA........to 10035????????/////

#include<stdio.h>
int main()
{
int a,b,count,carry,sum;
while(scanf("%d %d",&a,&b)==2&&(a||b!=0))
{count=0;carry=0;sum=0;
while(a>0||b>0)
{ sum=(a%10)+(b%10)+carry;
if(sum>=10) count+=1;
carry=((a%10)+(b%10)+carry)/10;
a=a/10;b=b/10;
}
if(count==0) printf("No carry operation.\n");
else if(count>0) printf("%d carry operation.\n",count);
}
return 0;
}    brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: why WA........to 10035????????/////

Do a diff against the sample output.

if(count==0) printf("No carry operation.\n");
else if(count==1) printf("1 carry operation.\n");
else printf("%d carry operations.\n",count);
Check input and AC output for thousands of problems on uDebug!

ahsanalishahid
New poster
Posts: 4
Joined: Thu Dec 29, 2011 12:03 am

### Re: UVA 10035

I have tried every possible test case i think of but judge is giving me wrong answer. Here is the code. Help me if I am wrong.

Code: Select all

``````#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <sstream>
#include <cstdlib>
using namespace std;

int main()
{

string number1 = "";
string number2 = "";
int sum = 0;
int n1 = 0, n2 = 0;
char c1 = 'n', c2 = 'n';
stringstream ss("");
string temp = "", answer = "";
int carry = 0;
int counter =0;
while( cin >> number1 >> number2 )
{
//ss.clear();
if(number1 == "0" && number2 == "0")
{
break;
}
//if (number1 != "0" || number2 != "0") break;

if(number1.length() > number2.length() )
{

while( number2.length() != number1.length() )
{
number2 = "0" + number2;
}

}
else if(number2.length() > number1.length() )
{
while( number1.length() != number2.length() )
{
number1 = "0" + number1;

}

}

//if(number1.length() < 10)
{
//cout << '2' << endl;
c1 = number1[j];
n1 = atoi(&c1);
//cout << number2 <<endl;
c2 = number2[j];
//cout << "ccc" << endl;
n2 = atoi(&c2);
sum = n2 + n1 + carry;
if(sum > 9)
{
int lastnum = sum / 10;
int remainder = sum %10;
carry = lastnum;
counter++;
//cout << "carry " << carry << endl;;
ss << remainder; ss >> temp;

temp.clear();
ss.clear();
}
else
{

ss << sum;
ss >> temp;
temp.clear();
ss.clear();
carry = 0;
}

if( j == 0 && carry !=  0)
{

ss << carry;
ss >> temp;

carry =0;

temp.erase();
ss.clear();

//j++;
}

}

if(counter == 0  )
{
if(number1 != "0" || number2 != "0")
{
cout << "No carry operation." << endl;
}
}
else if(counter== 1)
{
cout << counter << " carry operation." << endl;

}
else
{
cout << counter << " carry operations." << endl;
}
counter = 0;

number1.erase();
number2.erase();

}

cin.get();
cin.get();
return 0;

}
``````
I have tried all test cases that I have found on this board and its answer is correct but I am getting WA. I am freaking out :/ kindly help me.

Regards.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: UVA 10035

ahsanalishahid, the code you posted is not correct for the sample I/O.
Check input and AC output for thousands of problems on uDebug!

ahsanalishahid
New poster
Posts: 4
Joined: Thu Dec 29, 2011 12:03 am

### Re: UVA 10035

@brianfry, can u please point out what am I doing wrong as I am still unable to figure out.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: UVA 10035

For the first input 123 456, right after you execute "sum = n2 + n1 + carry", here's what's in your variables:
n1=3 n2=63
n1=2 n2=52
n1=14 n2=414

I wouldn't recommend calling atoi on a single char.
Check input and AC output for thousands of problems on uDebug!

ahsanalishahid
New poster
Posts: 4
Joined: Thu Dec 29, 2011 12:03 am

### Re: UVA 10035

@brianfry . THanks man! i used stringstream instead and it worked Thanks again 