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

Post Reply
kakashi
New poster
Posts: 5
Joined: Thu May 26, 2005 10:58 am

10035 Primary Arithmetic WA

Post by kakashi »

i don't understand why my program got WA.
plz help me :cry:
I can`t find which is wrong

Code: Select all

#include<iostream>
#include<cstring>

using namespace std;
int main(){
    char a[11],b[11];
    int lena,lenb;
    int i,j;
    for(cin >> a >> b ; cin ; cin >> a >> b){
    if(a[0]=='0'&&b[0]=='0')
        break;
    
    int count=0;
    lena=strlen(a);
    lenb=strlen(b);
    
    for(i=0;i<lena;i++){
            a[i]-='0';
    }
    for(i=0;i<lenb;i++){
            b[i]-='0';
    }
    for(i=lena,j=lenb;i>=0||j>=0;i--,j--){
                if((a[i]+b[j])>9){
                    if(i>=j){
                        if(i-1>=0){
                           a[i-1]+=1;
                        }
                    }
                    else{
                         if(j-1>=0){
                           b[j-1]+=1;
                        }
                    }    
                    count+=1;
                }
    }
        
        if(count==1)                                           
           cout << count << " carry operation.\n";
        else if(count>1)
           cout << count << " carry operations.\n";
        else
           cout << "No carry operation.\n";
    }
}
Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal »

Dear Kakashi,
your program fails for the following input set.

Code: Select all

9999 9999
0099 0099
99 99
123 987
0 0
kakashi
New poster
Posts: 5
Joined: Thu May 26, 2005 10:58 am

Post by kakashi »

I change the code...
but i still get WA
if(a[0]=='0'&&b[0]=='0'&&strlen(a)<2&&strlen(b)<2)
break;

:cry:
tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

Post by tan_Yui »

Hi, kakashi.
Try to check the following I/O.

Input :

Code: Select all

999 1
1 999
9999 0
0 9999
0 0
Output should be :

Code: Select all

3 carry operations.
3 carry operations.
No carry operation.
No carry operation.
Best regards.
Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal »

Kakashi,
Your modification can still fail if input is like this:

Code: Select all

0000 0000
Your program is supposed to stop at this point. So take care of this case.
I think tan_Yui's test cases are good. if you still get WA, then I would suggest you to rewrite the whole thing and using long long this time instead of array of char. It is simpler that way.
runtime
New poster
Posts: 3
Joined: Mon Jul 25, 2005 8:39 am
Contact:

10035 WA plz help me...

Post by runtime »

I have tried all the test data of ACM board
but it's still got WA....why??

Code: Select all

#include <stdio.h>
#include <stdlib.h>
int main(){
	char a[11],b[11];
	int i,j,k,m,n;
	while(1){
		scanf("%s %s",a,b);
		for(m=0;a[m]!='\0';m++);
		for(n=0;b[n]!='\0';n++);
		for(i=0,j=m-1;i<j;a[i]^=a[j]^=a[i]^=a[j],i++,j--);
		for(i=0,j=n-1;i<j;b[i]^=b[j]^=b[i]^=b[j],i++,j--);
		if((m==1)&&(n==1)&&(a[0]=='0')&&(b[0]=='0'))break;
		if(n>m){
			for(i=m;i<n;i++)a[i]='0';
			m=n;
		}else for(i=n;i<m;i++)b[i]='0';
		for(i=0;i<m;i++){
			a[i]-='0';
			b[i]-='0';
		}for(i=0,k=0;i<m;i++){
			if(a[i]+b[i]>9){
				k++;
				a[i+1]++;
			}
		}if(k==0)printf("No carry operation.\n");
		else printf("%d carry operation.\n",k);
	}return 0;
}
tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

Re: 10035 WA plz help me...

Post by tan_Yui »

Hi, it's careless mistakes.

Code: Select all

      }if(k==0)printf("No carry operation.\n");
      else if(k==1)printf("1 carry operation.\n");
      else printf("%d carry operations.\n",k);
After changed like above, you will get Accepted ! :wink:

Best regards.
sharmin_bd
New poster
Posts: 4
Joined: Tue Aug 30, 2005 6:42 pm

10035WA..Please Help!!!

Post by sharmin_bd »

Why this is getting wrong answer????
[/code]
#include<stdio.h>
#include<stdlib.h>

int bitcount(unsigned long n){
int count = 1;
while((n/=2)!=0){
count++;
}
return count;
}

void main(){
unsigned long x,y,temp;
int *in1,*in2;
int len1,len2,len,i,carry,count;
while(scanf("%u %u",&x,&y)){
carry = 0;
count = 0;
if(x == 0 && y == 0)
break;
len1 = bitcount(x);
len2 = bitcount(y);
len = (len1 > len2) ? len1 : len2;
in1 = (int*)malloc(len * sizeof(int));
in2 = (int*)malloc(len * sizeof(int));
for(i = 0;i<len;i++){
in1 = 0;
in2 = 0;
}
temp = x;
i = len-1;
while(temp!=0){
in1[i--] = temp%10;
temp/=10;
}
temp = y;
i = len-1;
while(temp!=0){
in2[i--] = temp%10;
temp/=10;
}
for(i = len-1;i>=0;i--){
if((in1+in2+carry) >=10){
carry = 1;
count++;
}
}
if(count == 0){
printf("No carry operation.\n");
}
else if(count > 1){
printf("%d carry operations.\n",count);
}
else{
printf("1 carry operation.\n");
}
}
}
helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

..

Post by helloneo »

try this..

Code: Select all

999999 1000000000000000
tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

Re: ..

Post by tan_Yui »

helloneo wrote:try this..

Code: Select all

999999 1000000000000000
Although my code doesn't support above input, it got Accepted.
Each line of input contains two unsigned integers less than 10 digits.
By the way, I also think about sharmin_bd's code, but I couldn't find the bugs... :(

Best regards.
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Try this input(taken from a past thread in the forum):

Input:
9090 10
0 0

Correct Output:
1 carry operation.

Your code gives:
2 carry operations.
sharmin_bd
New poster
Posts: 4
Joined: Tue Aug 30, 2005 6:42 pm

Post by sharmin_bd »

Thnx...I got AC
weirdo
New poster
Posts: 1
Joined: Sun Sep 18, 2005 3:13 am

10035-Primary Arithmetic

Post by weirdo »

oh~~~ :(
i tried any test data,but i still get wa.
tell me how to fix,plz. thx all~~~

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NO_DEBUG
int isExit(char*,char*);
void swapString(char* ,char*);
int main(int argc, char *argv[])
{
  int i;
  char number_1[10],number_2[10];
  while(1){
       int carry=0,flag=0;
       scanf("%s %s",number_1,number_2);
       if (isExit(number_1,number_2))
          break;
       if (strcmp(number_1,number_2)<0){
          swapString(number_1,number_2);

          #ifdef DEBUG
          printf("number_1 - %s\n",number_1);
          printf("number_2 - %s\n",number_2);
          #endif

       }
       int number_1_len=strlen(number_1);
       int number_2_len=strlen(number_2);
       int amount=(number_1_len>number_2_len)?number_2_len:number_1_len;

       #ifdef DEBUG
            printf("Number length - %d\n",amount);
       #endif

       for(i=1; i<=amount; i++){

           #ifdef DEBUG
           printf("%d -> %c\n",i,number_1[number_1_len-i]);
           printf("%d -> %c\n",i,number_2[number_2_len-i]);           
           #endif

           int sum=(number_1[number_1_len-i]+number_2[number_2_len-i])-96+flag;
           flag=0;

           #ifdef DEBUG
           printf("Total - %d\n",sum);
           #endif

           if (sum>=10){
              ++carry;
              flag=1;   
           }
       }

       #ifdef DEBUG
       printf("%d -> %c\n",i,number_1[number_1_len-i]);
       printf("flag - %d\n",flag);
       #endif

       while(flag==1 && number_1[number_1_len-i]=='9'){
          i++;
          carry++;
       }
       if (carry==0)
          puts("No carry operation.");
       else if(carry==1)
          puts("1 carry operation.");
       else
           printf("%d carry operations.\n",carry);
  }
  return 0;
}
int isExit(char* a,char* b){
    if ((strcmp(b,"0")==0) && (strcmp(b,a)==0))
       return 1;
    return 0;
}
void swapString(char* a,char* b){
     char temp[10];
     strcpy(temp,a);
     strcpy(a,b);
     strcpy(b,temp);
     return;
}
luishhh
New poster
Posts: 26
Joined: Mon Oct 25, 2004 8:11 pm
Location: Spain

10035 Primary Artihmetic

Post by luishhh »

Hi!
I have no idea why my code is wrong... if anybody finds a silly mistake please reply!

Code: Select all

/*Primary Arithmetic 10035 UVA*/
#include <stdio.h>

int main () {
int a, b;
int carries, mellevo;

for (;;) {
 scanf ("%u %u", &a, &b);
 if (a == 0 && b == 0) break;
 carries = 0;
 mellevo = 0;
 while (a > 0 || b > 0) {
   if (a % 10 + b % 10 + mellevo>= 10) { carries++; mellevo = 1;}
   else mellevo = 0;
   a = (a - a % 10) / 10;
   b = (b - b % 10) / 10;
 }
 if (!carries) printf("No carry operations.\n");
 else if (carries == 1) printf ("1 carry operation.\n");
 else printf ("%u carry operations.\n", carries);
}
return 0;
}
"From lost to the river" --> Spanish quote
thirddawn
New poster
Posts: 9
Joined: Sat Oct 15, 2005 4:41 am

10035 Unknow WA

Post by thirddawn »

Code: Select all

#include <stdio.h>

int main() {
	int a,b,a0,b0,counter=0;
	while (scanf("%d%d",&a,&b)==2){
		counter = 0;
        while (a>=10 && b>=10){
			a0=a%10;
			b0=b%10;
			if (a0+b0>=10)
				counter = counter + 1;
			a=a/10;
			b=b/10;
		}
		if (a<10){
			if (a+b%10>=10)
				counter = counter + 1;
		}
		else if (b<10){
			if (a%10+b>=10)
				counter = counter + 1;
		}
		if (counter==0)
			printf ("No carry operation.");
		else if (counter==1)
			printf ("1 carry operation.");
		else
			printf ("%d carry operations.",counter);
		if (a==0 && b==0)
			return 0;
	}
}
This is WA, eventhough the sample is ok.What's more, my classmate give me very more test numbers and it's ok with it.
thanks for helping :)
Post Reply

Return to “Volume 100 (10000-10099)”