C++编程问题(求速度!在线等)
#include<iostream>usingnamespacestd;doublefx(doublem,doublea,doubleb,doublec,doublef)...
#include<iostream>
using namespace std;
double fx(double m,double a,double b,double c,double f)
{
f=a*m*m+b*m+c;
return f;
}
double max(double m,double n,double a,double b,double c,double f,double M,double x)
{
while(m<=n)
{
if(fx(m,a,b,c,f)>M)
M=fx(m,a,b,c,f);
m=m+x;
}
return M;
}
double min(double m,double n,double a,double b,double c,double f,double M,double x)
{
while(m<=n)
{
if(fx(m,a,b,c,f)<M)
M=fx(m,a,b,c,f);
m=m+x;
}
return M;
}
double main()
{
double a,b,c,m,n,x,M,f;
cout<<"Input a,b,c:";
cin>>a>>b>>c;
cout<<"Input m,n:";
cin>>m>>n;
cout<<"Define the num:";
cin>>x;
M=fx(a,b,c,m,f);
int p;
cout<<"Choose the type of this program:max(1) or min(0):";
cin>>p;
switch(p)
{
case 1:
cout<<"Max:"<<max(m,n,M,a,b,c,f,x)<<endl;
break;
case 0:
cout<<"Min:"<<min(m,n,M,a,b,c,f,x)<<endl;
break;
}
return 0;
}
这是个求二次函数最大/最小的代码,我分析算法和语法都没问题啊,为什么运算结果就是不对啊?求大神帮我看看。 展开
using namespace std;
double fx(double m,double a,double b,double c,double f)
{
f=a*m*m+b*m+c;
return f;
}
double max(double m,double n,double a,double b,double c,double f,double M,double x)
{
while(m<=n)
{
if(fx(m,a,b,c,f)>M)
M=fx(m,a,b,c,f);
m=m+x;
}
return M;
}
double min(double m,double n,double a,double b,double c,double f,double M,double x)
{
while(m<=n)
{
if(fx(m,a,b,c,f)<M)
M=fx(m,a,b,c,f);
m=m+x;
}
return M;
}
double main()
{
double a,b,c,m,n,x,M,f;
cout<<"Input a,b,c:";
cin>>a>>b>>c;
cout<<"Input m,n:";
cin>>m>>n;
cout<<"Define the num:";
cin>>x;
M=fx(a,b,c,m,f);
int p;
cout<<"Choose the type of this program:max(1) or min(0):";
cin>>p;
switch(p)
{
case 1:
cout<<"Max:"<<max(m,n,M,a,b,c,f,x)<<endl;
break;
case 0:
cout<<"Min:"<<min(m,n,M,a,b,c,f,x)<<endl;
break;
}
return 0;
}
这是个求二次函数最大/最小的代码,我分析算法和语法都没问题啊,为什么运算结果就是不对啊?求大神帮我看看。 展开
1个回答
展开全部
二次函数最大值最小值看a的正负,正的有最小值,负的有最大值。你没有对a进行判断啊。不能让用户选择最大最小。最大值、最小值有公式的:(4ac-b^2)/4a。不用这么麻烦的。
自己写了一个
#include <iostream>
using namespace std;
float coculate(float a,float b,float c)
{
return (4*a*c-b*b)/(4*a);
}
int main()
{
float a=0,b=0,c=0; //系数
cout<<"输入a,b,c"<<endl;
cin>>a>>b>>c;
if(a>0)
{
cout <<"有最小值:"<<coculate(a,b,c)<<endl;
}
else if(a<0)
{
cout <<"有最大值:"<<coculate(a,b,c)<<endl;
}
else
{
cout <<"输入错误"<<endl;
}
return 0;
}
自己写了一个
#include <iostream>
using namespace std;
float coculate(float a,float b,float c)
{
return (4*a*c-b*b)/(4*a);
}
int main()
{
float a=0,b=0,c=0; //系数
cout<<"输入a,b,c"<<endl;
cin>>a>>b>>c;
if(a>0)
{
cout <<"有最小值:"<<coculate(a,b,c)<<endl;
}
else if(a<0)
{
cout <<"有最大值:"<<coculate(a,b,c)<<endl;
}
else
{
cout <<"输入错误"<<endl;
}
return 0;
}
追问
我不用公式,我想求的是任意用户定义的范围内的最大最小,其中x为计算精度,用m=m+x来步步计算而求得范围内各各点的值,然后找出最大最小。(练技术才这么干的,不然我就用#define了)
追答
好吧。。既然这样。
double fx(double m,double a,double b,double c,double f)
M=fx(a,b,c,m,f);
double min(double m,double n,double a,double b,double c,double f,double M,double x)
min(m,n,M,a,b,c,f,x)
double max(double m,double n,double a,double b,double c,double f,double M,double x)
max(m,n,M,a,b,c,f,x)
你的这几个函数算法没有问题。但是传参传乱了。自己改一下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询