![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
请设计计算法:求一元二次方程ax^2+bx+c=0 的根 考虑有两个相等实根和两个不相等实根的情况
3个回答
展开全部
#include <iostream>
#include <cmath>
using namespace std;
int Solve(double a,double b,double c,double* x1,double* x2);
int main()
{
double a,b,c,x1,x2;
cout<<"Input a, b, c: ";
cin >> a >> b >> c;
int res = Solve(a, b, c, &x1, &x2);
cout<<"The real roots are: ";
if (res == 1)
{
cout << "两个不等实根:x1=" << x1 << " x2=" << x2 << endl;
}
else if (res == 0)
{
cout << "两个相等实根:x1=" << x1 << " x2=" << x2 << endl;
}
else
{
cout << "没有实根" << endl;
}
return 0;
}
int Solve(double a,double b,double c,double* x1,double* x2)
{
double det = b * b - 4 * a * c;
if (det < 0)
{
return -1;
}
else if (det > 0)
{
*x1 = (-b + sqrt(det)) / (2 * a);
*x2 = (-b - sqrt(det)) / (2 * a);
return 1;
}
*x1 = *x2 = (-b + sqrt(det)) / (2 * a);
return 0;
}
#include <cmath>
using namespace std;
int Solve(double a,double b,double c,double* x1,double* x2);
int main()
{
double a,b,c,x1,x2;
cout<<"Input a, b, c: ";
cin >> a >> b >> c;
int res = Solve(a, b, c, &x1, &x2);
cout<<"The real roots are: ";
if (res == 1)
{
cout << "两个不等实根:x1=" << x1 << " x2=" << x2 << endl;
}
else if (res == 0)
{
cout << "两个相等实根:x1=" << x1 << " x2=" << x2 << endl;
}
else
{
cout << "没有实根" << endl;
}
return 0;
}
int Solve(double a,double b,double c,double* x1,double* x2)
{
double det = b * b - 4 * a * c;
if (det < 0)
{
return -1;
}
else if (det > 0)
{
*x1 = (-b + sqrt(det)) / (2 * a);
*x2 = (-b - sqrt(det)) / (2 * a);
return 1;
}
*x1 = *x2 = (-b + sqrt(det)) / (2 * a);
return 0;
}
展开全部
一元二次方程ax^2+bx+c=0
因为是一元二次方程,所以a不等于0.
一元二次方程的根的判别式是△=b^2-4ac
△>0说明方程有两个不同实数解,x=(-b+√△)/2a 或x=(-b-√△)/2a
△=0说明方程有两个相等实数解,x=(-b+√△)/2a
△<0说明方程无实数解。
因为是一元二次方程,所以a不等于0.
一元二次方程的根的判别式是△=b^2-4ac
△>0说明方程有两个不同实数解,x=(-b+√△)/2a 或x=(-b-√△)/2a
△=0说明方程有两个相等实数解,x=(-b+√△)/2a
△<0说明方程无实数解。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<math.h>
int f(double a,double b,double c,double *x1,double *x2)
{
double d=b*b-4*a*c;
if(d>0)
{
*x1=(-b+sqrt(d))/(2*a);
*x2=(-b-sqrt(d))/(2*a);
return 1;
}
else if(d==0)
{
*x1=*x2=(-b)/(2*a);
return 0;
}
else
return -1;
}
void main()
{ double a1,a2,a3,y1,y2,t;
printf("请输入系数\n",);
while(scanf("%lf%lf%lf",&a1,&a2,&a3)!=EOF)
{
t=f(a1,a2,a3,&y1,&y2);
if(t>0)
printf("两个实根分别是:x1=%.10f tx2=%.10f\n",y1,y2);
else if(t==0)
printf("两相等实根是 :x1=x2=%.4f\n",y1);
else printf("此方程无解!");
}
}
#include<math.h>
int f(double a,double b,double c,double *x1,double *x2)
{
double d=b*b-4*a*c;
if(d>0)
{
*x1=(-b+sqrt(d))/(2*a);
*x2=(-b-sqrt(d))/(2*a);
return 1;
}
else if(d==0)
{
*x1=*x2=(-b)/(2*a);
return 0;
}
else
return -1;
}
void main()
{ double a1,a2,a3,y1,y2,t;
printf("请输入系数\n",);
while(scanf("%lf%lf%lf",&a1,&a2,&a3)!=EOF)
{
t=f(a1,a2,a3,&y1,&y2);
if(t>0)
printf("两个实根分别是:x1=%.10f tx2=%.10f\n",y1,y2);
else if(t==0)
printf("两相等实根是 :x1=x2=%.4f\n",y1);
else printf("此方程无解!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询