三分求极值
链接在此
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
const double eps = 1e-7;
using namespace std;
int a,b,c,x,y;
double equ(double n){
return sqrt((n - x) * (n - x) + (a * n * n +b * n + c - y) * (a * n * n +b * n + c - y));
}
int main(){
cin>>a>>b>>c>>x>>y;
double l = -200,r = 200;
double mid = (l + r) / 2;
double midmid = (r + mid) / 2;
while(r - l > eps){
if(equ(mid) > equ(midmid)){
l = mid ;
}
else{
r = midmid ;
}
mid = (l + r) / 2;
midmid = (r + mid) / 2;
}
printf("%.3f\n",equ(midmid));
return 0;
}
二分解方程
链接在此处
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
long double mid,l=0,r=100,y;
mid=(l+r)/2;
cin>>y;
while((r-l)>1e-8) {
if(mid*mid*mid*mid*8+7*mid*mid*mid+2*mid*mid+3*mid+6-y>0) {
r=mid;
} else if(mid*mid*mid*mid*8+7*mid*mid*mid+2*mid*mid+3*mid+6-y<0) {
l=mid;
}
else break;
mid=(l+r)/2;
}
if(6-y<=0 && 100*100*100*100*8+7*100*100*100+2*100*100+3*100+6-y>=0)
cout<<fixed<<setprecision(4)<<mid<<endl;
else cout<<"No solution!"<<endl;
}
return 0;
}