标签:turn abs stream mes 原因 int iostream main ble
这是一道版子题
一开始看了别人的博客,知道三分方法
后来自己实现
交了两次,全WA。原因是系数定义成了int......唉样例竟然过了
第三次就全AC了
没啥思维含量,不过学到了 fabs() 和 判断精度
今天xmz问我一本通的
也用到了精度判断
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#define precision 1e-12
using namespace std;
int n ;
double a , b , l , r,xi[15];
double f(double x)
{
double x0 = 1;
double ans = 0;
for (int i = 0 ; i <= n ; i++)
{
x0 = 1;
int j = i;
while (j--)
{
x0 *= x;
}
x0 *= xi[i];
ans += x0;
}
//cout << " " << x << " " << ans << endl;
return ans;
}
int main()
{
cin >> n;
cin >> a >> b;
for (int i = n ; i >= 0 ; i--)
{
cin >> xi[i];
}
l = a , r = b;
while (l < r)
{
if(fabs(r - l) < precision)
break;
double thi1 = l + (r - l) / 3;
double thi2 = l + (r - l) / 3 * 2;
//cout << l << " " << thi1 << " " << thi2 << " " << r << endl;
if(f(thi1) < f(thi2))
{
l = thi1;
//cout << "left" << endl;
}
else
{
r = thi2;
//cout << "right" << endl;
}
}
printf ("%.5lf\n" , l);
return 0;
}
标签:turn abs stream mes 原因 int iostream main ble
原文地址:https://www.cnblogs.com/ZhengkunJia/p/12207900.html