HDOJ 2899 Strange fuction
原来科学计数法可以直接使用的,料想不到。折半查找,加上数学求导,很容易搞定。
View Code
1 #include <math.h>
2 #include <stdio.h>
3
4 double derivative(double x){
5 return 42.0*pow(x,6.0)+48.0*pow(x,5.0)+21.0*pow(x,2.0)+10.0*x;
6 }
7 double reuslt(double x,double y){
8 return 6.0*pow(x,7.0)+8.0*pow(x,6.0)+7.0*pow(x,3.0)+5.0*pow(x,2.0)-y*x;
9 }
10
11 int main()
12 {
13 //freopen("Strange fuction.txt","r",stdin);
14 unsigned num;
15 double y;
16 scanf("%d",&num);
17 while(num--)
18 {
19 scanf("%lf",&y);
20 if(y<derivative(100))
21 {
22 double start=0,end=100,half;
23 while(end-start>1e-6)
24 {
25 half=(start+end)/2;
26 if(derivative(half)<y)
27 start=half;
28 else
29 end=half;
30 }
31 printf("%.4lf\n",reuslt((start + end)/2,y));
32 }
33 else
34 printf("%.4lf\n",reuslt(100.0,y));
35 }
36 }