HDU 2899 Strange fuction

Strange fuction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 778    Accepted Submission(s): 597


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
 
 
 
题意是求最小值。
和HDU2199差不多。。。。。那里是原方程的解。。。。这里先求下导。。。。。然后找到极小值点就OK。。。。
 
 
 
View Code
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>

using namespace std;
//Constant Declaration
/*
--------------------------*/
//#define LL long long
#define LL __int64
const int M=1000000;
const int INF=1<<30;
const double EPS = 1e-11;
const double PI = acos(-1.0);
/*--------------------------*/
// some essential funtion
/*
----------------------------------*/
void Swap(int &a,int &b){ int t=a;a=b;b=t; }
int Max(int a,int b){ return a>b?a:b; }
int Min(int a,int b){ return a<b?a:b; }
int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
/*----------------------------------*/
//for (i = 0; i < n; i++)
/*
----------------------------------*/
#define F(x) (42*pow(x,6) + 48*pow(x,5) + 21*pow(x,2) + 10*x - y)
#define ans(x) (6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x)
double y;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t, case1 = 0;
scanf("%d", &t);
int n, m;
int i, j;
//scanf("%d%d", &n, &m);

while (t--)
{
scanf("%lf", &y);
if (F(0.0) * F(100.0) > 0)
{
if (F(0.0) > 0)
{
printf("%.4lf\n", ans(0.0));
}
else
{
printf("%.4lf\n", ans(100.0));
}
}
else
{
double l = 0, r = 100;
if (F(l) == 0)
{
printf("%.4lf", ans(0.0));
goto end;
}
if (F(r) == 0)
{
printf("%.4lf", ans(100.0));
goto end;
}

double min;
while (r - l > EPS)
{
min = (l + r) / 2;
if (F(min) == 0)
{
goto end;
}
if (F(l) * F(min) < 0)
{
r = min;
}
else
{
l = min;
}


}
end :
printf("%.4lf\n", ans(min));




}
}

return 0;
}
原文地址:https://www.cnblogs.com/qiufeihai/p/2408201.html