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代码

  1. #include<iostream>
  2. #include<iomanip>
  3. #include<stdio.h>
  4. #include<cmath>
  5. using namespace std;
  6. double f_d(double res)
  7. {
  8. return res*res*res*res *res*res*42 + res*res*res*res*res*48 + res*res*21 + res * 10;
  9. }
  10. double f(double res,double y){
  11. 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;
  12. }
  13. int main(){
  14. //freopen("date.in", "r", stdin);
  15. //freopen("date.out", "w", stdout);
  16. int T;
  17. double a;
  18. double b,e,tem;
  19. cin>>T;
  20. for(int i=0;i<T;i++){
  21. cin>>a;
  22. if(f_d(100)<=a)
  23. cout<<fixed<<setprecision(4)<<f(100,a)<<endl;
  24. else
  25. if(f_d(0)>=a)
  26. cout<<fixed<<setprecision(4)<<f(0,a)<<endl;
  27. else{
  28. b = 0, e = 100, tem = 50;
  29. while (fabs(f_d(tem) - a) >= 1e-7)
  30. if (f_d(tem)>a){
  31. e = tem;
  32. tem = (b + e) / 2;
  33. }
  34. else{
  35. b = tem;
  36. tem = (b + e) / 2;
  37. }
  38. cout<<fixed<<setprecision(4)<<f(tem,a)<<endl;
  39. }
  40. }
  41. }




原文地址:https://www.cnblogs.com/liuzhanshan/p/5405747.html