## 11001 - Necklace

All about problems in Volume 110. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am

### Re: 11001 - Necklace

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;
}
``````
@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

### 11001 - Necklace

Getting WA....plzz help me find the error

Code: Select all

``Code removed after AC``
Last edited by @ce on Mon Jun 25, 2012 10:03 am, edited 2 times in total.
-@ce
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11001 - Necklace

In my AC code I used double, not float. I also use an epsilon of 1e-12 to compare doubles.
Check input and AC output for thousands of problems on uDebug!
@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

### Re: 11001 - Necklace

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..
-@ce
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11001 - Necklace

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.
Check input and AC output for thousands of problems on uDebug!
@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

### Re: 11001 - Necklace

Thanks brianfry...i got AC
-@ce
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 11001 - Necklace

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11001 - Necklace

Check input and AC output for thousands of problems on uDebug!
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 11001 - Necklace

Gotcha. Thanks so much for sharing. This is wonderful information!
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.