Page 7 of 22
10035 Primary Arithmetic WA
Posted: Mon Aug 22, 2005 5:42 am
by kakashi
i don't understand why my program got WA.
plz help me
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";
}
}
Posted: Mon Aug 22, 2005 6:43 am
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
Posted: Mon Aug 22, 2005 9:45 am
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;

Posted: Mon Aug 22, 2005 4:10 pm
by tan_Yui
Hi, kakashi.
Try to check the following I/O.
Input :
Output should be :
Code: Select all
3 carry operations.
3 carry operations.
No carry operation.
No carry operation.
Best regards.
Posted: Thu Aug 25, 2005 10:58 am
by Raiyan Kamal
Kakashi,
Your modification can still fail if input is like this:
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.
10035 WA plz help me...
Posted: Thu Aug 25, 2005 2:30 pm
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;
}
Re: 10035 WA plz help me...
Posted: Thu Aug 25, 2005 4:09 pm
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 !
Best regards.
10035WA..Please Help!!!
Posted: Tue Aug 30, 2005 6:47 pm
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");
}
}
}
..
Posted: Tue Aug 30, 2005 8:29 pm
by helloneo
Re: ..
Posted: Wed Aug 31, 2005 4:49 am
by tan_Yui
helloneo wrote:try this..
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.
Posted: Wed Aug 31, 2005 10:07 am
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.
Posted: Wed Aug 31, 2005 7:47 pm
by sharmin_bd
Thnx...I got AC
10035-Primary Arithmetic
Posted: Sun Sep 18, 2005 3:33 am
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;
}
10035 Primary Artihmetic
Posted: Thu Sep 22, 2005 11:49 pm
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;
}
10035 Unknow WA
Posted: Sat Oct 22, 2005 2:31 am
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
