Lv.1
一个蒟蒻的OIer(洛谷:mengmeng123456)
签名:洛谷:mengmeng123456 (蒟蒻)
在 我们可以互相讨论 例题 中回复
这道题困扰了我好长时间:
题目描述 求一元二次方程ax 2+bx+c=0的根,其中a不等于0。结果要求精确到小数点后5位。 输入 输入一行,包含三个浮点数a, b, c(它们之间以一个空格分开),分别表示方程的系数。 输出 输出一行,表示方程的解。若两个实根相等,则输出形式为:“x1=x2=...“;若两个实根不等,在满足根小者在前的原则,则输出形式为:“x1=...;x2=...“;若无实根输出“No answer!”。-15.97 19.69 12.02 样例输出
x1=-0.44781;x2=1.68075
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
double a,b,c,d,x1,x2;
cin>>a>>b>>c;
if(b*b-4*a*c>=-1e-6&&b*b-4*a*c<=1e-6){
d=sqrt(b*b-4*a*c);
x1=(0-b+d)/(2*a);
x2=(0-b-d)/(2*a);
cout<<"x1=x2=";
printf("%.5f",x1);
}
else if(b*b-4*a*c>0){
d=sqrt(b*b-4*a*c);
x1=(0-b+d)/(2*a);
x2=(0-b-d)/(2*a);
if(x1<x2){
cout<<"x1=";
printf("%.5f",x1);
cout<<";"<<"x2=";
printf("%.5f",x2);
}
else{
cout<<"x1=";
printf("%.5f",x2);
cout<<";"<<"x2=";
printf("%.5f",x1);
}
}
else{
cout<<"No answer!";
}
return 0;
}
2023-04-08T16:00:23 点赞:0
在 https://shequ.codemao.cn/work/175066617 中回复
https://shequ.codemao.cn/work/175066617
2023-04-16T22:05:14 点赞:0