Home > Back-end >  A novice for help! Why program right?
A novice for help! Why program right?

Time:02-07

Recently is to follow a book - "the fifth edition of informatics orsay a tong (c + + edition)" learning c + +, book has a problem as follows:
[title description]
Using the formula of the x1=? B + b2? 2 a 4 ac), x2=? b? B2? 2 a 4 ac), a yuan quadratic equation ax2 + bx + c=0, among them a is not equal to zero, the result requires accurate to five decimal places,

[enter]
Input line, contains three floating point Numbers a, b, c (separated with a space between them), and respectively equation coefficient of ax2 + bx + c=0,

[output]
The output of a line, said equation solution,

If two real root is equal, the output in the form of: "x1 x2==... ";

If two real root, before meet outsiders in the principle of output in the form of: "x1=... ; X2=... ";

If there is No real root output "No answer!" ,

All output part requirements given to five decimal places, Numbers, symbols, there is no space between

[input sample]
15.97 19.69 12.02
[output sample]
X1=0.44781; X2=1.68075

My program is as follows:
# include & lt; Cstdio>
# include & lt; Cmath>
using namespace std;
Int main ()
{
Double a, b, c, SQRTN, x1, x2,//create the ABC constant,
Lf the scanf (" % % % lf lf ", & amp; A, & amp; B, & amp; C);
SQRTN=(b * b) - (4 * a * c);
If (SQRTN & lt; 0)
{
Printf (" % s ", "No answer!" );
}
The else
{
X1=(SQRT (SQRTN) - b)/(2 * a);
X2=(b (-) - SQRT (SQRTN))/(2 * a);
If (x1==x2)
{
Printf (" % s % 5 lf ", "x1 x2==", the x1);
}
The else
{
If (x1 & lt; X2)
{
Printf (" % s % 5 lf % s %. 5 lf ", "x1=", x1, "; X2="x2);
}
The else
{
Printf (" % s % 5 lf % s %. 5 lf ", "x1=", x2, "; X2=", the x1);
}
}
}
return 0;
}

The book has a matching evaluation website http://ybt.ssoier.cn:8088/, I hand up the my program after it tested with ten group number ten times, nine other turned on, there is a set of number it says wrong answer, I can't see what goes wrong, I looked back and saw the code confused  , consult everybody here a great god!

Hope everybody bosses answer, thank!!!!!! Thank you!!!!!!

CodePudding user response:

Reference:
///input sample 
//- 2 5 0
///output sample
//the x1 x2=0.00000=2.50000

# include & lt; Cstdio>
# include & lt; Cmath>
using namespace std;
Int main ()
{
Double a, b, c, SQRTN, x1, x2,//create the ABC constant,
Lf the scanf (" % % % lf lf ", & amp; A, & amp; B, & amp; C);
If (a==0) return 1;
SQRTN=(b * b) - (4 * a * c);
If (SQRTN & lt; 0)
{
Printf (" % s ", "No answer!" );
}
If (SQRTN==0)
{
Printf (" % s % 5 lf ", "x1 x2==", (b)/(2 * a) + 0.0000001);////prevent output - 0.00
the condition of the}
If (SQRTN & gt; 0)
{
X1=(SQRT (SQRTN) - b)/(2 * a) + 0.0000001.
X2=(b (-) - SQRT (SQRTN))/(2 * a) + 0.0000001.
If (x1 & lt; X2)
{
Printf (" % s % 5 lf % s %. 5 lf ", "x1=", x1, "; X2="x2);
}
The else
{
Printf (" % s % 5 lf % s %. 5 lf ", "x1=", x2, "; X2=", the x1);
}
}

return 0;
}
  • Related