acm课程练习2--1002
题目描述
Now, here is a fuction:
F(x) = 6 * x^7+8x^6+7x^3+5x^2-yx (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
大意
求函数在区间[0,100]的最小值
思路
对函数求导得F’(x)=42* x^6+48x^5+21x^2+10x-y
,再对导函数求导,发现导函数是单调递增的。
得到结论,函数的最小值点为
42 x^6+48x^5+21x^2+10*x=y的根。
程序应首先判断根是否在[0,100]区间内,分为三种情况讨论。
这一题的代码与第一题差别不大,是第一题的变形
AC代码
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<cmath>
using namespace std;
double f_d(double res)
{
return res*res*res*res *res*res*42 + res*res*res*res*res*48 + res*res*21 + res * 10;
}
double f(double res,double y){
return res*res*res*res*res*res*res*6 + res*res*res*res*res*res*8 + res*res*res*7 + res*res* 5-res*y;
}
int main(){
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int T;
double a;
double b,e,tem;
cin>>T;
for(int i=0;i<T;i++){
cin>>a;
if(f_d(100)<=a)
cout<<fixed<<setprecision(4)<<f(100,a)<<endl;
else
if(f_d(0)>=a)
cout<<fixed<<setprecision(4)<<f(0,a)<<endl;
else{
b = 0, e = 100, tem = 50;
while (fabs(f_d(tem) - a) >= 1e-7)
if (f_d(tem)>a){
e = tem;
tem = (b + e) / 2;
}
else{
b = tem;
tem = (b + e) / 2;
}
cout<<fixed<<setprecision(4)<<f(tem,a)<<endl;
}
}
}