2014年7月21日 星期一

UVA-12097, Pie

浮點數的題目要特別小心部分
精確位數要取好
尤其是跟圓周率有關的題目
千萬不要自己打3.1416
要用math.h算acos(-1.0)...

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstdlib>

 4 #include <cmath>

 5 #define PI acos(-1.0)

 6 using namespace std;

 7 int main(){

 8     int T, n, f, tmp;

 9     double p[10001];

10     double maxp, L, R, M;

11     int piece;

12     scanf("%d", &T);

13     while(T--){

14         scanf("%d%d", &n, &f);

15         f+=1;

16         maxp=0;

17         for(int i=0 ; i<n ; ++i){

18             scanf("%d", &tmp);

19             p[i]=1.*tmp*tmp*PI;

20             if(p[i]>maxp)

21                 maxp=p[i];

22         }

23         L=0.; R=maxp;

24         while(R-L>1e-7){

25             M=(L+R)/2;

26             piece=0;

27             for(int i=0 ; i<n ; ++i){

28                 piece+=floor(p[i]/M);

29             }

30             if(piece < f){

31                 R=M;

32             }else{

33                 L=M;

34             }

35             

36         }

37         printf("%.5f\n", L);

38         

39     }

40 return 0;

41 }



沒有留言:

張貼留言

TEST