1 条题解
-
0
C :
#include <stdio.h> #include <math.h> int main() { double a,b,c,disc,x1,x2; while(~scanf("%lf%lf%lf",&a,&b,&c)) { disc = b * b - 4 * a * c; if(disc > 0) { x1 = (-b - sqrt(disc)) / (2 * a); x2 = (-b + sqrt(disc)) / (2 * a); printf("x1=%.3f x2=%.3f\n",x1,x2); } else if(disc == 0) { x1 = -b / (2 * a); printf("x1=%.3f\n",x1); } else { double p = -b / (2 * a); double q = sqrt(-disc) / (2 * a); printf("x1=%.3f+%.3fi x2=%.3f-%.3fi\n",p,q,p,q); } } return 0; }
C++ :
#include<iostream> #include<cmath> #include<cstdio> using namespace std; double disc,x1,x2,p,q; void big(int a,int b) //>0 { x1=(-b+sqrt(disc))/(2.0*a); x2=(-b-sqrt(disc))/(2.0*a); } void small(int a,int b) //<0 { p=-1.0*b/(2*a); q=sqrt(-disc)/(2*a); } void equal(int a,int b) //==0 { x1=x2=-1.0*b/(2*a); } int main() { int a,b,c; cin>>a>>b>>c; disc=b*b-4*a*c; if (disc>0) { big(a,b); printf("x1=%.3lf x2=%.3lf\n",x1,x2); } else if (disc==0) { equal(a,b); printf("x1=x2=%.3lf\n",x1); } else { small(a,b); printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",p,q,p,q); } return 0; }
- 1
信息
- ID
- 1512
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者