ACM : HDU 2899 Strange fuction 解题报告 -二分、三分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5933 Accepted Submission(s): 4194 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. 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 <br><br> //三分二分都可以,导数单调,求导后二分值 AC代码 |

1 #include"iostream" 2 #include"algorithm" 3 #include"cstdio" 4 #include"cstring" 5 #include"cmath" 6 #define max(a,b) a>b?a:b 7 #define min(a,b) a<b?a:b 8 #define MX 10000 + 50 9 using namespace std; 10 double y; 11 12 double f(double x) { //求导 13 return 42*pow(x,6)+48*pow(x,5)+21*x*x+10*x-y; // F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) 14 } 15 16 double F(double x) { 17 return 6 * pow(x,7)+8*pow(x,6)+7*x*x*x+5*x*x-y*x;// F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) 18 } 19 20 int main() { 21 int T; 22 while(cin>>T) { 23 for(int qq=0; qq<T; qq++) { 24 cin>>y; 25 if(f(0)>0) { 26 printf("%.4f\n",F(0)); 27 } else if(f(100)<0) { 28 printf("%.4f\n",F(100)); 29 } else { 30 double l=0; // 0 l r 100 31 double r=100; // ------------------------- 32 double mid; 33 while(r-l>1e-8) { 34 mid=(r+l)/2.0; 35 if(f(mid)>0) { 36 r=mid; 37 } else { 38 l=mid; 39 } 40 } 41 printf("%.4f\n",F(mid)); 42 } 43 } 44 } 45 return 0; 46 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步