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

route
New poster
Posts: 39
Joined: Sat Dec 21, 2002 1:25 am

Re: WA 10035

Post by route »

I didn't test your program..
but here's my suggestion that one can easily get wrong

999
1
4 carry operations.

or to increase the array of char

or when the length of the second number is greater than that of the first number.

Check if u a correct.

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

Looks like you don't clear up your arrays before any addition ... eventhough your solution survives the first test-case, it's hard to believe it would pass subsequent cases ... try this test cases:

91 2
1 12
0 0

Both test-cases doesn't have carry operations ...

-turuthok-

Derk
New poster
Posts: 23
Joined: Mon Mar 17, 2003 3:53 am
Location: Louisville, KY
Contact:

I get WA too

Post by Derk »

I've tried every sample input, every input in all the threads here. I try max and mins, and all kings of different values in between and I still can't get accepted. It runs for a little bit before it gets WA, so I'm doing something right...

[c]/* @JUDGE_ID: xxxxxx 10035 C*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFFERSIZE 30

main()
{
char buffer[BUFFERSIZE], n1[12], n2[12];
int carries, c, l1, l2,max,min,i,diff,a,b;
#ifndef ONLINE_JUDGE
close (0); open ("10035.in", O_RDONLY);
close (1); open ("10035.out", O_WRONLY | O_CREAT, 0600);
#endif

while(1)
{
gets(buffer);

sscanf(buffer,"%d %d",&a,&b);

if (a==0 && b==0 && strlen(buffer)<4)
return;

sprintf(n1, "%d",a);
sprintf(n2, "%d",b);

l1 = strlen(n1);
l2 = strlen(n2);

carries=0;
c=0;

if (l1>l2)
{
max=l1;
min=l2;
diff=max-min;

while(max-->min)
n2[max]=n2[max-diff];
max = l1;
n2[max]=0x00;
for(i=0;i<diff;i++)
n2='0';
}
else
{
max=l2;
min=l1;
diff=max-min;

while(max-->min)
n1[max]=n1[max-diff];
max = l2;
n1[max]=0x00;

for(i=0;i<diff;i++)
n1='0';
}


while (max>0)
{
max--;
if (c+n1[max]-'0'+n2[max]-'0'>9)
{
c=1;
carries++;
}
else
c=0;
}

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

}

}

/* @END_OF_SOURCE_CODE */[/c]

Derk
New poster
Posts: 23
Joined: Mon Mar 17, 2003 3:53 am
Location: Louisville, KY
Contact:

Nevermind...

Post by Derk »

Nevermind... I worked it a different way, without all the strings and stuff. Got accepted.

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric »

I get AC. Thx~

SoLtRiX
New poster
Posts: 10
Joined: Tue Dec 03, 2002 1:17 am
Location: Portugal
Contact:

Post by SoLtRiX »

there are no need to use strings. i use unsigned long long int and i've got AC.

be carefull width inputs width only one zero
123 0
0 2

Derk
New poster
Posts: 23
Joined: Mon Mar 17, 2003 3:53 am
Location: Louisville, KY
Contact:

Um

Post by Derk »

as I said already, nevermind, I got AC. Also, you can't use unsigned long long ints in C. Different programming language. Also, you don't even need that -- a simple int will suffice. Anything else is overkill.

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

Post by Almost Human »

I got problem too... please help me ... I still got WA

Code: Select all

#include <stdio.h>

int main ( )
{
	unsigned long operand1 , operand2 , result ;
	int carry , carryflag ;

	freopen ( "10035.in" , "r" , stdin ) ;
	freopen ( "10035.out" , "w" , stdout ) ;

	while ( 1 )
	{
	 scanf ( "%lu %lu" , &operand1 , &operand2 ) ;

	 if ( !operand1 && !operand2 ) break ;

	 carryflag = carry = 0 ;

	 do
	 {
		 if ( carryflag ) carry ++ ;

		 result = ( operand1 % 10 ) + ( operand2 % 10 ) + carryflag ;
		 operand1 /= 10 ; operand2 /= 10 ;

		 carryflag = result / 10 ;
	 }
	 while ( result ) ;

	 if ( !carry )
		 printf ( "No " ) ;
	 else
		 printf ( "%i " , carry ) ;
	 printf ( "carry operation" ) ;

	 if ( carry > 1 ) printf ( "s.\n" ) ;
	 else printf ( ".\n" ) ;
	}

	return 0 ;
}

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka »

hello almost human.....

based on your code you cannot handle input like this.

Code: Select all

12300 78900
output should be

Code: Select all

2 carry operations.
Just a little mistake. :wink:

rumel_new
New poster
Posts: 3
Joined: Tue Feb 25, 2003 5:44 pm
Location: Dhaka

Post by rumel_new »

Why i got WA
here is my code:
[cpp]

Code: Select all

#include<stdio.h>
#include<stdlib.h>
void main ()
{
unsigned long x,y;
unsigned long carry,temp_carry,temp_x,temp_y;	
while(2 == scanf("%lu %lu",&x,&y))
{
if((x == 0) && (y == 0))
break;
else
{
carry =0;
temp_carry = 0;
while(x)
{
temp_x = x%10;
x = x / 10;
temp_y = y%10;
y = y / 10;
temp_carry = (temp_carry + temp_x + temp_y) /10;
if(temp_carry>0)
carry++;
}
if(carry == 1)
printf("1 carry operation.\n");
else if(carry > 1)
printf("%lu carry operations.\n",carry);
else
printf("No carry operation.\n");
}
}
}
[/cpp]
We are SKARBAN.
We will make another world.

awik_10
New poster
Posts: 14
Joined: Sun May 18, 2003 11:56 am

10035

Post by awik_10 »

can you tell me what's my wrong? i've tried some exceptions like 988+15-->3 carry operations, 188+15--> 2 carry operations, and the other one is 100+0--> No carry operation. tell me if there's something wrong :( .

thank you.
i'm waiting for your replies.
best regards,
awik
Last edited by awik_10 on Mon May 19, 2003 9:17 am, edited 1 time in total.

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

Layout problem again...... :P

1. Line 106 should be removed.
2. Pay EXTRA attention to the output format......
123 456
0 0
Correct answer:
No carry operation.
Relevent line in your code:
[c] if(ctr==0) printf("No carry operation\n"); [/c]


******* Think ***********************
************ Think ******************
***************** Think *************





P.S. Again, after some minor amendment, I got an ACC with your code.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

awik_10
New poster
Posts: 14
Joined: Sun May 18, 2003 11:56 am

Post by awik_10 »

ya, i've seen what's my fault is. it is because my output without a dot(.), but i've still been seen as a presentation error, not correctly at all. can you tel me the other fault, besides dot that make my program P.E.
thank you
best regards,
awik

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Post by Observer »

Observer wrote:1. Line 106 should be removed.
Have you done that also?
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

awik_10
New poster
Posts: 14
Joined: Sun May 18, 2003 11:56 am

Post by awik_10 »

thank you.
awik

Post Reply

Return to “Volume 100 (10000-10099)”