解方程(二)
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
- 描述
-
Now, here is a fuction:
F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.- 输入
- The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
- 输出
- Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
- 样例输入
-
2 100 200
- 样例输出
-
-74.4291 -178.8534
-
一开始看到题目也是比较没有头绪,后来经人提醒知道了方法,求出导数,因为其导数的正负可以决定方程的单调性而导数的最大最小值正负其实很大一部分是由Y决定的,所以先判断导数最大最小值,最大值小于零,说明最小值为F(100),最小值大于零,说明F(0)为最小值,有正有负需要寻找零点,零点的寻找利用二分查找法。
#include<stdio.h> #include<math.h> double fun1(double n,double y) { return 6*pow(n,7)+8*pow(n,6)+7*pow(n,3)+5*pow(n,2)-y*n; } double fun2(double n,double y) { return 42*pow(n,6)+48*pow(n,5)+21*pow(n,2)+10*n-y; } int main() { double n,i,y; double fun1(double n,double y); double fun2(double n,double y); scanf("%lf",&n); while(n--) { scanf("%lf",&y); double number1=42*pow(100,6)+48*pow(100,5)+21*pow(100,2)+10*100; if(y>number1) { printf("%.4lf\n",fun1(100,y)); continue; } if(y<=0) { printf("%.4lf\n",fun1(0,y)); continue; } double a=0,b=100,c=fun2(0,y),d=fun2(100,y),e; while((b-a)>1e-10) { e=(a+b)/2; if(fabs(fun2(e,y))<1e-10) break; else if(fun2(e,y)*c<0) { d=fun2(e,y); b=e; } else { c=fun2(e,y); a=e; } } printf("%.4lf\n",fun1(e,y)); } return 0; }