65,186
社区成员




#include <stdio.h>
#include <math.h>
int main()
{
for (double a, b, c, d, x1, x2; scanf("%lf%lf%lf", &a, &b, &c) == 3;)
if ((d = b * b - 4 * a * c) < 0 || fabs(a) < 1E-15)
puts("No answer!");
else
{
x1 = (-b + sqrt(d)) / a / 2, x2 = (-b - sqrt(d)) / a / 2;
d > 0 ?
printf("x1=%.5f;x2=%.5f\n", x1 <= x2 ? x1 : x2, x1 <= x2 ? x2 : x1) :
printf("x1=x2=%.5f\n", x1);
}
return 0;
}
# include <cstdio>
# include <cmath>
#define EXP 0.00000001
using namespace std;
int main()
{
double a, b, c, sqrtn, x1, x2;//创建abc常数,
scanf("%lf %lf %lf", &a, &b, &c);
if((a >= -EXP)&&(a <= EXP)) return -1;
sqrtn = (b * b) - (4 * a * c);
if(( sqrtn >= -EXP)&&( sqrtn <= EXP))
{
printf("x1=x2=%.5lf",(-b)/(2*a) + EXP);
}
else if(sqrtn > 0.0)
{
x1 = (-b + sqrt(sqrtn))/(2 * a) + EXP;
x2 = (-b - sqrt(sqrtn))/(2 * a) + EXP;
if (x1 < x2)
{
printf("x1=%.5lf;x2=%.5lf",x1,x2);
}
else
{
printf("x1=%.5lf;x2=%.5lf",x2,x1);
}
}
else
{
printf("No answer!");
}
return 0;
}
//【输入样例】
//-2 5 0
//【输出样例】
//x1=-0.00000 x2=2.50000
# include <cstdio>
# include <cmath>
using namespace std;
int main()
{
double a, b, c, sqrtn, x1, x2;//创建abc常数,
scanf("%lf %lf %lf", &a, &b, &c);
if(a==0)return -1;
sqrtn = (b * b) - (4 * a * c);
if (sqrtn < 0)
{
printf("%s", "No answer!");
}
if(sqrtn == 0)
{
printf("%s%.5lf", "x1=x2=", (-b)/(2*a)+0.0000001);////防止输出-0.00的情况
}
if(sqrtn > 0)
{
x1 = (sqrt(sqrtn) - b) / (2 * a)+ 0.0000001;
x2 = ((-b) - sqrt(sqrtn)) / (2 * a)+0.0000001;
if (x1 < x2)
{
printf("%s%.5lf%s%.5lf", "x1=", x1, ";x2=", x2);
}
else
{
printf("%s%.5lf%s%.5lf", "x1=", x2, ";x2=", x1);
}
}
return 0;
}