2 条题解
-
2
https://www.luogu.com.cn/blog/SodiumCarbonate/b2053-ti-xie
讲解见代码!
求根公式题目已经描述了
#include<iostream> #include<cmath> #include<cstdio> using namespace std; double a,b,c,x1,x2,derta;//定义变量 #define N 1e-6//浮点数不能直接比较大小!必须留一个小误差,这题留1e-6就够了 int main(){ cin>>a>>b>>c; derta=b*b-4*a*c;//计算derta if(derta<-N){//derta是负数,此方程无解! printf("No answer!"); return 0; } if(abs(derta)<N){//derta为0. //此处特别注意!一定不能直接写derta==0!否则会因为浮点误差而挂掉 printf("x1=x2=%.5lf",(-b)/(2*a));//输出相等的根 return 0; } if(derta>N){//有两个解 double sqrt_derta=sqrt(derta);//先把derta开根 x1=((-b)+sqrt_derta)/(2*a);//求根 x2=((-b)-sqrt_derta)/(2*a);//求根 if(x1>x2) swap(x1,x2);///从小到大输出!必须swap一下!我就是这里没swapWA了3个点 printf("x1=%.5lf;x2=%.5lf",x1,x2);//输出两个解 return 0;//完结撒花! } //望管理大大给过! }
(自洛谷账号转载,TJ没过审)
-
1
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main(){ double a,b,c; cin >> a >> b >> c; double x1=((0-b)+sqrt(b*b-4*a*c))/(2*a); double x2=((0-b)-sqrt(b*b-4*a*c))/(2*a); if(x1<x2){ printf("x1=%.5lf;x2=%.5lf",x1,x2); }else if(x1>x2){ printf("x1=%.5lf;x2=%.5lf",x2,x1); }else if(x1==x2){ printf("x1=x2=%.5lf",x1); }else{ cout << "No answer!"; } return 0; }
- 1
信息
- ID
- 6865
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- (无)
- 递交数
- 47
- 已通过
- 21
- 上传者