Files
workspace/cpp/algo/p3382.cpp
2026-01-31 14:38:00 +08:00

49 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
/*
double calc(const vector<double>& func, int x){
int n = func.size();
double ans = 0;
for(int i = 0; i < n; i++){
ans += pow(x, n-i) * func[i];
}
return ans;
}*/
//秦九韶算法
double calc(const vector<double>& func, double x){
double ans = 0;
for(double y: func){
ans = x * ans + y;
}
return ans;
}
int main(){
int n;
double l, r;
n = 3; l = -0.9981; r = 0.5;
//cin >> n >> l >> r;
//任取两个点,把[l, r]分成三段调整l, r的值
vector<double> func(n+1);
func[0] = 1; func[1] = -3; func[2] = -3; func[3] = 1;
/*for(int i = 0; i < n; i++){
}*/
double mid1, mid2;
double ans = 0;
while(r - l > 1e-6){
mid1 = l + (r-l)/3;
mid2 = r - (r-l)/3;
if(calc(func, mid1) > calc(func, mid2)){
r = mid2;
} else if(calc(func, mid1) <= calc(func, mid2)) {
l = mid1;
}
}
cout << fixed << setprecision(8) << l << endl;
return 0;
}