Page 3 of 3

Re: 11001 - Necklace

Posted: Thu May 01, 2008 8:19 am
by roticv
Anyone know why I'm getting WA?

Code: Select all

#include <stdio.h>
#include <math.h>

int v,v0;
double n,n0,n1,tmp1, tmp2;

int main(){
	while (scanf("%d %d ",&v,&v0)!=EOF){
		if (v0 == 0 && v == 0)
			break;
		if (v0>=v){
			printf("0\n");
			continue;
		}
		n = v;
		n /= v0;
		n /= 2.0;
		n0 = floor(n);
		n1 = ceil(n);
		if (n0 == n1){
			printf("%d\n",(int)n0);
			continue;
		}
		tmp1 = v; tmp2 = v;
		if (n0==0.0)
			tmp1 = 0.0;
		else
			tmp1 = (tmp1/n0) - v0;
		tmp2 = (tmp2/n1) - v0;
		tmp1 = n0*sqrt(tmp1);
		tmp2 = n1*sqrt(tmp2);
		if (tmp1 == tmp2){
			printf("0\n");
			continue;
		}
		if (tmp1 < tmp2)
			printf("%d\n",(int)n1);
		else
			printf("%d\n",(int)n0);
	}
	return 0;
}

11001 - Necklace

Posted: Thu Jun 14, 2012 12:29 am
by @ce
Getting WA....plzz help me find the error

Code: Select all

Code removed after AC

Re: 11001 - Necklace

Posted: Thu Jun 14, 2012 9:50 pm
by brianfry713
In my AC code I used double, not float. I also use an epsilon of 1e-12 to compare doubles.

Re: 11001 - Necklace

Posted: Fri Jun 15, 2012 2:05 pm
by @ce
Using double didn't get my code AC..
What do u mean by "epsilon of 1e-12"....cann u plz tell me what it is with syntax..

Re: 11001 - Necklace

Posted: Fri Jun 15, 2012 9:48 pm
by brianfry713
If a and b are doubles, to test if a==b:
if(fabs(a-b)<1e-12)
http://acm.uva.es/p/float-in-competition.pdf

Input 26145 11, AC output is 1188.

Re: 11001 - Necklace

Posted: Sat Jun 16, 2012 8:48 am
by @ce
Thanks brianfry...i got AC :)

Re: 11001 - Necklace

Posted: Wed Jan 01, 2014 8:55 am
by uDebug
brianfry713 wrote:If a and b are doubles, to test if a==b:
if(fabs(a-b)<1e-12)
http://acm.uva.es/p/float-in-competition.pdf

Input 26145 11, AC output is 1188.
Wow! Thanks so much. I was really tripping up on comparing two values that looked similar and what you wrote is a really valuable.

Unfortunately, the above link no longer works.

Re: 11001 - Necklace

Posted: Wed Jan 15, 2014 12:49 am
by brianfry713

Re: 11001 - Necklace

Posted: Wed Jan 15, 2014 6:48 am
by uDebug
Gotcha. Thanks so much for sharing. This is wonderful information!