## 10035 - Primary Arithmetic

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

mobarak.islam
New poster
Posts: 38
Joined: Wed Dec 05, 2012 11:29 pm

### Re: 10035 - Primary Arithmetic

Here i'm geting WA!! Please help

#include<stdio.h>
#include<stdlib.h>

int main()
{
long long int a,b;
scanf("%lld %lld",&a,&b);
label:
while(a!=0 && b!=0)
{
int c,d,count=0,fcount=0;
while((a/10+b/10)!=0)
{
c=a%10;
d=b%10+count;
if((c+d)>9)
{
count=1;
fcount++;
}
else
count=0;
a=a/10;
b=b/10;
}
if((a+b+count)>9)
{
count=1;
fcount++;
}
if(fcount!=0)
printf("\n%d carry operations.",fcount);
else
printf("\nNo carry operation.");

scanf("%lld %lld",&a,&b);
goto label;
}
return 0;
}

@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

### Re: 10035 - Primary Arithmetic

@ mobarak.islam
Input

Code: Select all

``````1 9
0 0``````
Your Output

Code: Select all

``1 carry operations.``
AC Output

Code: Select all

``1 carry operation.``
-@ce

rtperson
New poster
Posts: 3
Joined: Fri Aug 22, 2008 2:53 pm

### Re: 10035 - Primary Arithmetic

Can someone suggest a test case that would show me why the following code is getting WA? I've tried all the test cases I can find, and it seems to work fine for them:

Code: Select all

``code removed after AC``
I've tried the following test cases (from a previous post)

Code: Select all

``````123 456
555 555

123 594
1234 5678910
12354 9125478654
321456 21
56324
987
1 9
333 0
0 1
11 99

9999999999
9999999999
9999999999 1

99999999 1
0 0``````
And received the following output:

Code: Select all

``````No carry operation.
3 carry operations.
1 carry operation.
2 carry operations.
3 carry operations.
No carry operation.
3 carry operations.
1 carry operation.
No carry operation.
No carry operation.
2 carry operations.
10 carry operations.
10 carry operations.
8 carry operations.``````
Obviously, there's something I'm missing. I appreciate any help you can offer. Thanks!
Last edited by rtperson on Thu Jan 17, 2013 10:28 pm, edited 2 times in total.

mobarak.islam
New poster
Posts: 38
Joined: Wed Dec 05, 2012 11:29 pm

### Re: 10035 - Primary Arithmetic

@ce , I fixed the problem for 1 carry operation. but still I'm getting WA #include<stdio.h>
#include<stdlib.h>

int main()
{
long long int a,b;
scanf("%lld %lld",&a,&b);
label:
while(a!=0 && b!=0)
{
int c,d,count=0,fcount=0;
while((a/10+b/10)!=0)
{
c=a%10;
d=b%10+count;
if((c+d)>9)
{
count=1;
fcount++;
}
else
count=0;
a=a/10;
b=b/10;
}
if((a+b+count)>9)
{
count=1;
fcount++;
}
if(fcount>1)
printf("%d carry operations.\n",fcount);
else if(fcount==1)
printf("%d carry operation.\n",fcount);
else
printf("No carry operation.\n");

scanf("%lld %lld",&a,&b);
goto label;
}
return 0;
}

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

### Re: 10035 - Primary Arithmetic

rtperson wrote:Can someone suggest a test case that would show me why the following code is getting WA? I've tried all the test cases I can find, and it seems to work fine for them:

Code: Select all

``````9 9
5 4
0 0``````
Check input and AC output for thousands of problems on uDebug!

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

### Re: 10035 - Primary Arithmetic

mobarak.islam wrote:@ce , I fixed the problem for 1 carry operation. but still I'm getting WA Try input:

Code: Select all

``````0 1
0 0``````
output should be 1
Check input and AC output for thousands of problems on uDebug!

rtperson
New poster
Posts: 3
Joined: Fri Aug 22, 2008 2:53 pm

### Re: 10035 - Primary Arithmetic

Brianfry, thank you so much. I should have known it was that simple. Just got AC. Much obliged. I'll remove the code now.

top90233a
New poster
Posts: 1
Joined: Tue Feb 19, 2013 8:13 am

### 10035 wrong answer

wrong answer... plz help

Code: Select all

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

int main(){

char int1,int2;
char tmp1,tmp2;

int i;

int carry;

int carrysum;

char tmp;

memset(tmp1,'0',10);
memset(tmp2,'0',10);

while(scanf("%s %s",tmp1,tmp2)==2){

if(strcmp(tmp1,"0")==0 && strcmp(tmp2,"0")==0)	break;

carrysum = 0;
carry = 0;

memset(int1,'0',10);
memset(int2,'0',10);

for(i=0 ; i<strlen(tmp1) ; i++){
int1[i+10-strlen(tmp1)] = tmp1[i];
}

for(i=0 ; i<strlen(tmp2) ; i++){
int2[i+10-strlen(tmp2)] = tmp2[i];
}

for(i=9 ; i>=0 ; i--){

if(int1[i]=='0'){
if(int2[i]=='9' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='1'){
if(int2[i]=='9'){
carry = 1;
carrysum++;
}
else if(int2[i]=='8' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='2'){
if(int2[i]=='9' || int2[i]=='8'){
carry = 1;
carrysum++;
}
else if(int2[i]=='7' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='3'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7'){
carry = 1;
carrysum++;
}
else if(int2[i]=='6' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='4'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6'){
carry = 1;
carrysum++;
}
else if(int2[i]=='5' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='5'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6' || int2[i]=='5'){
carry = 1;
carrysum++;
}
else if(int2[i]=='4' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='6'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6' || int2[i]=='5' || int2[i]=='4'){
carry = 1;
carrysum++;
}
else if(int2[i]=='3' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='7'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6' || int2[i]=='5' || int2[i]=='4' || int2[i]=='3'){
carry = 1;
carrysum++;
}
else if(int2[i]=='2' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='8'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6' || int2[i]=='5' || int2[i]=='4' || int2[i]=='3' || int2[i]=='2'){
carry = 1;
carrysum++;
}
else if(int2[i]=='1' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}

else if(int1[i]=='9'){
if(int2[i]=='9' || int2[i]=='8' || int2[i]=='7' || int2[i]=='6' || int2[i]=='5' || int2[i]=='4' || int2[i]=='3' || int2[i]=='2' || int2[i]=='1'){
carry = 1;
carrysum++;
}
else if(int2[i]=='0' && carry == 1){
carry = 1;
carrysum++;
}
else carry = 0;
}
}

if		(carrysum == 0)	printf("No carry operation.\n");
else if	(carrysum == 1)	printf("1 carry operation.\n");
else					printf("%d carry operations.\n",carrysum);

memset(tmp1,'0',10);
memset(tmp2,'0',10);
}

return 0;
}``````

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

### Re: 10035 wrong answer

That is AC code.
Check input and AC output for thousands of problems on uDebug!

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

### Re: 10035 - Primary Arithmetic

Deleted
Last edited by raj on Tue Mar 05, 2013 11:50 pm, edited 1 time in total.

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

### Re: 10035 - Primary Arithmetic

You are missing the s at the end of "3 carry operations." in the sample I/O.

Use the code tags when you post code.
Check input and AC output for thousands of problems on uDebug!

CyberPunk
New poster
Posts: 7
Joined: Sat Mar 02, 2013 11:11 pm

### Re: 10035 - Primary Arithmetic

I tried every single I/O on this forum and got correct for all of them with the following code, which by the way gets WA.

Code: Select all

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

int main()
{
char num1, num2;
int i,j,count,l,min,x,y;
int sum;
while(scanf("%s %s", num1, num2)==2 && !(num1=='0' && num1==NULL && num2=='0' && num2==NULL)) {
count=0;

int len_num1=strlen(num1), len_num2=strlen(num2);

if(len_num1>=len_num2) min=len_num2;
else min=len_num1;
for(; min; min--,len_num1--,len_num2--) {
i=num1[len_num1-1]-48;
j=num2[len_num2-1]-48;
sum=i+j;

if(sum>9) {
count++;
if(len_num1-2>=0 && (num1[len_num1-2]-48)!=9) num1[len_num1-2]++;
else if(len_num2-2>=0 && (num2[len_num2-2]-48)!=9) num2[len_num2-2]++;
else continue;
}
}

while(len_num1>0 && sum>9) {
if((num1[len_num1-1]-48)==9) {
count++;
sum=10;
if(len_num1-2>=0 && (num1[len_num1-2]-48)!=9) {
sum=0;
}
}
else sum=0;
len_num1--;
}

while(len_num2>0 && sum>9) {
if((num2[len_num2-1]-48)==9) {
count++;
sum=10;
if(len_num2-2>=0 && (num2[len_num2-2]-48)!=9) {
sum=0;
}
}
else sum=0;
len_num2--;
}

if(!count) printf("No carry operation.\n");
else if(count==1) printf("%d carry operation.\n", count);
else if(count>1) printf("%d carry operations.\n", count);
}
return 0;
}
``````
help needed. N.B.: I am a newby, so my code might look very silly. please ignore its look.

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

### Re: 10035 - Primary Arithmetic

CyberPunk wrote:I tried every single I/O on this forum and got correct for all of them with the following code, which by the way gets WA.
You may try the following cases:

Input

Code: Select all

``````362768463 890
865 194758626
841 841998503
73537331 897098126
88441456 899193277
0 0
``````
Output

Code: Select all

``````2 carry operations.
2 carry operations.
1 carry operation.
4 carry operations.
5 carry operations.
``````

CyberPunk
New poster
Posts: 7
Joined: Sat Mar 02, 2013 11:11 pm

### Re: 10035 - Primary Arithmetic

Thank you very much. got AC! da729vid
New poster
Posts: 2
Joined: Mon May 20, 2013 9:21 pm

### Re: 10035 - Primary Arithmetic

What is wrong with my problem? I got wrong answer.

My code:

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{

int num1,num2,count,add;

cin>>num1>>num2;
while(num1||num2)
{
count=0;
add=0;

while(num1||num2)
{
add=(num1%10+num2%10+add)/10;
if(add)
{
count++;
}

num1/=10;
num2/=10;
}

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

else if(count==1)
cout<<"1 carry operation."<<endl;

else
cout<<count<<" carry operations."<<endl;

cin>>num1>>num2;

}

return 0;

}

thanks