HDU 2899 Strange fuction 二分
Strange fuction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5429 Accepted Submission(s): 3886
Problem Description
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.
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.
Input
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)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534
Author
Redow
Recommend
lcy
题目大意:第一行T,接下来输入T个Y,求方程F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)最小值。
思路:求导,把导函数等于0的X值计算出来,代入原方程就是最小值。(好像有什么三分......没写,下次写下)
数学方法:
#include <iostream> #include <math.h> double y; double vv(double x) { return (6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y); } double v(double x) { return (42*pow(x, 6)+48*pow(x, 5)+21*pow(x, 2)+10*x-y); } double f(double x,double z,double y) { double mid; while((y-z)>1e-10) { mid = (y+z)/2; if(v(mid)<x) z=mid+1e-10; else y=mid-1e-10; } return mid; } int main() { int a, T; scanf("%d", &T); while(T--) { scanf("%lf", &y); printf("%.4lf\n", vv(f(0,0,100))); } return 0; }
三分:
#include <iostream> #include <stdio.h> #include <math.h> using namespace std; double f(double x, double y) { return (6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-x*y); } int main() { int a, T, t; double mid1,mid2, z, y; cin>>T; while(T--) { z=0;y=100;t=50; cin>>a; while(t--){ mid1=(z+y)/2; mid2=(mid1+y)/2; if(f(mid1,a)<f(mid2,a)) y=mid2; else z=mid1; } printf("%.4lf\n", f(mid1,a)); } return 0; }
posted on 2016-03-28 22:51 2855669158 阅读(251) 评论(0) 编辑 收藏 举报