10035 - Primary Arithmetic
Moderator: Board moderators
-
- 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;
}
#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;
}
Re: 10035 - Primary Arithmetic
@ mobarak.islam
Input
Your Output
AC Output
Input
Code: Select all
1 9
0 0
Code: Select all
1 carry operations.
Code: Select all
1 carry operation.
-@ce
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:
I've tried the following test cases (from a previous post)
And received the following output:
Obviously, there's something I'm missing. I appreciate any help you can offer. Thanks!
Code: Select all
code removed after AC
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
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.
Last edited by rtperson on Thu Jan 17, 2013 10:28 pm, edited 2 times in total.
-
- 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;
}

#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;
}
-
- 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!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10035 - Primary Arithmetic
Try input:mobarak.islam wrote:@ce , I fixed the problem for 1 carry operation. but still I'm getting WA![]()
Code: Select all
0 1
0 0
Check input and AC output for thousands of problems on uDebug!
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.
10035 wrong answer
wrong answer... plz help
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char int1[10],int2[10];
char tmp1[10],tmp2[10];
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;
}
-
- 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!
Re: 10035 - Primary Arithmetic
Deleted
Last edited by raj on Tue Mar 05, 2013 11:50 pm, edited 1 time in total.
-
- 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.
Use the code tags when you post code.
Check input and AC output for thousands of problems on uDebug!
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.
help needed. 
N.B.: I am a newby, so my code might look very silly. please ignore its look.
Code: Select all
#include <stdio.h>
#include <string.h>
int main()
{
char num1[32767], num2[32767];
int i,j,count,l,min,x,y;
int sum;
while(scanf("%s %s", num1, num2)==2 && !(num1[0]=='0' && num1[1]==NULL && num2[0]=='0' && num2[1]==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;
}

N.B.: I am a newby, so my code might look very silly. please ignore its look.
Re: 10035 - Primary Arithmetic
You may try the following cases: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.
Input
Code: Select all
362768463 890
865 194758626
841 841998503
73537331 897098126
88441456 899193277
0 0
Code: Select all
2 carry operations.
2 carry operations.
1 carry operation.
4 carry operations.
5 carry operations.
Re: 10035 - Primary Arithmetic
Thank you very much. got AC! 

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
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