最近做了些二分的题目总结下二分吧 突然发现零点定理也很适合二分。二分需要注意的便是精度和退出的条件有时候可以把二分的次数设为定值然后用一个变量来保存等循环结束即得到了最优答案
零点定理型
Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
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 a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2
100
-4
Sample Output
1.6152
No solution!
这个函数在给定的定义域内是单调的所以上来变通过两端点判定是否有零点否则就不断地换区间就行了。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
double go(int x)
{
double c,d,e,l=0,r=100,mid;
while(1)
{
mid=(l+r)/2;
c=8*pow(l,4)+7*pow(l,3)+2*pow(l,2)+3*l+6-x;
d=8*pow(r,4)+7*pow(r,3)+2*pow(r,2)+3*r+6-x;
e=8*pow(mid,4)+7*pow(mid,3)+2*pow(mid,2)+3*mid+6-x;
if(c*d>0)
return -1;
if(fabs(e)<1e-4)
return mid;
if(c*e<0)
r=mid;
if(d*e<0)
l=mid;
}
}
int main()
{
int n,m;
cin>>n;
while(n--)
{
cin>>m;
if(go(m)==-1)
cout<<"No solution!"<<endl;
else
{
printf("%.4f\n",go(m));
}
}
}
Now, here is a fuction:
F(x) = 6 * x7+8*x6+7x3+5*x2-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
这个题和上面的体一样先求导倒数的零点就是最小值(倒数是增的零点左负右正极值就是最小值)革命尚未成功同志仍需努力