1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
n分法求最小值最优测试.cpp 1010 Bytes
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#include <iostream>
#include <windows.h>
using namespace std;
const double inf=0x7fffffff;
int time=1;
double func(double x,double a,double b,double c){
return a*x*x+b*x+c;
}
int cac(int n,double a,double b,double c,double l,double r,double ans){
time = 1;
double x[n];
double y[n];
do{
time++;
for(int i=1;i<=n;i++)
x[i-1] = i*(r-l)/n+l;
for(int i=0;i<n;i++)
y[i] = func(x[i],a,b,c);
double min=inf;
int index=-1;
for(int i=0;i<n;i++){
if(y[i] < min){
index = i+1;
min = y[i];
}
}
double left,right;
left = (index-1)*(r-l)/n+l;
right = (index+1)*(r-l)/n+l;
l = left;
r = right;
}while(r-l > 0.001);
if(l-ans > 0.001 || l-ans < -0.001)
return -1;
else
return time*n;
}
int main(){
freopen("output.txt","w",stdout);
while(true){
double a,b,c;
cin>>a>>b>>c;
double l,r,ans;
cin>>l>>r>>ans;
for(int i=3;i<=15;i++)
cout<<cac(i,a,b,c,l,r,ans)<<" ";
cout<<endl;
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385