Strange fuction HDU
阿新 • • 發佈:2019-02-11
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. InputThe 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) OutputJust the minimum value (accurate up to 4 decimal places),when x is between 0 and 100. Sample Input
Sample Output
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. InputThe 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) OutputJust the minimum value (accurate up to 4 decimal places),when x is between 0 and 100. Sample Input
2 100 200
-74.4291 -178.8534
看了大神的題解(沒錯我就是每道題都要看題解的菜菜)才知道要用求導算,得自己解,不能讓程式解。虧我之前還傻乎乎的想讓程式給我求出來……hehe
:
#include<stdio.h> #include<math.h> double df(double x) //定義double型別函式,宣告變數x { return 42*pow(x,6)+48*pow(x,5)+21*x*x+10*x; //返回這個函式的一階導數 -(y*x)沒了 } double f(double x,double y)//再定義一個函式, { return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x; //返回原函式 } int main() { int T, i; double y, left, right, mid; //二分 scanf("%d", &T); while(T--) { scanf("%lf",&y); left = 0, right = 100; //x的範圍 for(i = 1; i <= 100; i++) //三分法 { mid = (left + right) / 2; if(df(mid) > y) right = mid; else left = mid; } printf("%.4lf\n", f(mid, y)); } return 0; }