import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Created on 07/10/14.
* Ax2 + Bx + C = 0.
Your task is to find the number of distinct roots of the equation and print all of them in ascending order.
Input
The first line contains three integer numbers A, B and C ( - 10^5 ≤ A, B, C ≤ 10^5). Any coefficient may be equal to 0.
Output
In case of infinite root count print the only integer -1. In case of no roots print the only integer 0.
In other cases print the number of root on the first line and the roots on the following lines in the ascending order.
Print roots with at least 5 digits after the decimal point.
*/
public class Main {
public static final String FORMAT
= "%.5f"; public static final double ZERO = 0;
private void solve(double a, double b, double c) {
// consider 8 cases
// 000
// infinite # of roots
if (a == 0 && b == 0 && c == 0) {
return;
}
// 001
// equation: c = 0 where c != 0 doesn't have roots
if (a == 0 && b == 0 && c != 0) {
return;
}
// 010
// bx = 0, one zero root
if (a == 0 && b != 0 && c == 0) {
System.
out.
format(FORMAT, ZERO
); return;
}
// 011
// bx + c = 0
// one root
if (a == 0 && b != 0 && c != 0) {
System.
out.
format(FORMAT,
-c
/ b
); return;
}
// 100
// ax^2 = 0
// one distinct root
if (a != 0 && b == 0 && c == 0) {
System.
out.
format(FORMAT, ZERO
); return;
}
// 101
// ax^2 + c = 0
if (a != 0 && b == 0 && c != 0) {
if (a * c < 0) {
}
else {
}
return;
}
// 110
// ax^2 + bx = 0
if (a != 0 && b != 0 && c == 0) {
if (-b / a > 0) {
System.
out.
format(FORMAT, ZERO
); System.
out.
format(FORMAT,
-b
/ a
); }
else {
System.
out.
format(FORMAT,
-b
/ a
); System.
out.
format(FORMAT, ZERO
); }
return;
}
// 111
// ax^2 + bx + c = 0
if (a != 0 && b != 0 && c != 0) {
double d = b * b - 4 * a * c;
if (d < 0) {
// no real roots
}
else if (d > 0){
double ans1
= (-b
- Math.
sqrt(d
)) / (2 * a
); double ans2
= (-b
+ Math.
sqrt(d
)) / (2 * a
); if (ans2 > ans1) {
System.
out.
format(FORMAT, ans1
); System.
out.
format(FORMAT, ans2
); }
else {
System.
out.
format(FORMAT, ans2
); System.
out.
format(FORMAT, ans1
); }
}
else {
System.
out.
format(FORMAT,
(-b
/ (2 * a
))); }
return;
}
}
public static void main
(String[] args
) { Scanner reader
= new Scanner
(System.
in); double a = reader.nextInt();
double b = reader.nextInt();
double c = reader.nextInt();
Main task = new Main();
task.solve(a, b, c);
}
}
aW1wb3J0IGphdmEuaW8uUHJpbnRTdHJlYW07CmltcG9ydCBqYXZhLnRleHQuRGVjaW1hbEZvcm1hdDsKaW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwogCi8qKgogKiBDcmVhdGVkIG9uIDA3LzEwLzE0LgogKiBBeDLigIkr4oCJQnjigIkr4oCJQ+KAiT3igIkwLgogWW91ciB0YXNrIGlzIHRvIGZpbmQgdGhlIG51bWJlciBvZiBkaXN0aW5jdCByb290cyBvZiB0aGUgZXF1YXRpb24gYW5kIHByaW50IGFsbCBvZiB0aGVtIGluIGFzY2VuZGluZyBvcmRlci4KIAogSW5wdXQKIFRoZSBmaXJzdCBsaW5lIGNvbnRhaW5zIHRocmVlIGludGVnZXIgbnVtYmVycyBBLOKAiUIgYW5kIEMgKOKAiS3igIkxMF414oCJ4omk4oCJQSzigIlCLOKAiUPigIniiaTigIkxMF41KS4gQW55IGNvZWZmaWNpZW50IG1heSBiZSBlcXVhbCB0byAwLgogCiBPdXRwdXQKIEluIGNhc2Ugb2YgaW5maW5pdGUgcm9vdCBjb3VudCBwcmludCB0aGUgb25seSBpbnRlZ2VyIC0xLiBJbiBjYXNlIG9mIG5vIHJvb3RzIHByaW50IHRoZSBvbmx5IGludGVnZXIgMC4KIEluIG90aGVyIGNhc2VzIHByaW50IHRoZSBudW1iZXIgb2Ygcm9vdCBvbiB0aGUgZmlyc3QgbGluZSBhbmQgdGhlIHJvb3RzIG9uIHRoZSBmb2xsb3dpbmcgbGluZXMgaW4gdGhlIGFzY2VuZGluZyBvcmRlci4KIFByaW50IHJvb3RzIHdpdGggYXQgbGVhc3QgNSBkaWdpdHMgYWZ0ZXIgdGhlIGRlY2ltYWwgcG9pbnQuCiAqLwpwdWJsaWMgY2xhc3MgTWFpbiB7CiAKICAgIHB1YmxpYyBzdGF0aWMgZmluYWwgU3RyaW5nIEZPUk1BVCA9ICIlLjVmIjsKICAgIHB1YmxpYyBzdGF0aWMgZmluYWwgZG91YmxlIFpFUk8gPSAwOwogCiAKICAgIHByaXZhdGUgdm9pZCBzb2x2ZShkb3VibGUgYSwgZG91YmxlIGIsIGRvdWJsZSBjKSB7CiAgICAgICAgLy8gY29uc2lkZXIgOCBjYXNlcwogCiAgICAgICAgLy8gMDAwCiAgICAgICAgLy8gaW5maW5pdGUgIyBvZiByb290cwogICAgICAgIGlmIChhID09IDAgJiYgYiA9PSAwICYmIGMgPT0gMCkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oLTEpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogCiAgICAgICAgLy8gMDAxCiAgICAgICAgLy8gZXF1YXRpb246IGMgPSAwIHdoZXJlIGMgIT0gMCBkb2Vzbid0IGhhdmUgcm9vdHMKICAgICAgICBpZiAoYSA9PSAwICYmIGIgPT0gMCAmJiBjICE9IDApIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKDApOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogCiAgICAgICAgLy8gMDEwCiAgICAgICAgLy8gYnggPSAwLCBvbmUgemVybyByb290CiAgICAgICAgaWYgKGEgPT0gMCAmJiBiICE9IDAgJiYgYyA9PSAwKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigxKTsKICAgICAgICAgICAgU3lzdGVtLm91dC5mb3JtYXQoRk9STUFULCBaRVJPKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KIAogICAgICAgIC8vIDAxMQogICAgICAgIC8vIGJ4ICsgYyA9IDAKICAgICAgICAvLyBvbmUgcm9vdAogICAgICAgIGlmIChhID09IDAgJiYgYiAhPSAwICYmIGMgIT0gMCkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oMSk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgLWMgLyBiKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KIAogICAgICAgIC8vIDEwMAogICAgICAgIC8vIGF4XjIgPSAwCiAgICAgICAgLy8gb25lIGRpc3RpbmN0IHJvb3QKICAgICAgICBpZiAoYSAhPSAwICYmIGIgPT0gMCAmJiBjID09IDApIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKDEpOwogICAgICAgICAgICBTeXN0ZW0ub3V0LmZvcm1hdChGT1JNQVQsIFpFUk8pOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogCiAgICAgICAgLy8gMTAxCiAgICAgICAgLy8gYXheMiArIGMgPSAwCiAgICAgICAgaWYgKGEgIT0gMCAmJiBiID09IDAgJiYgYyAhPSAwKSB7CiAgICAgICAgICAgIGlmIChhICogYyA8IDApIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigyKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgLSBNYXRoLnNxcnQoLWMgLyBhKSk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgTWF0aC5zcXJ0KC1jIC8gYSkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKDApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAKICAgICAgICAvLyAxMTAKICAgICAgICAvLyBheF4yICsgYnggPSAwCiAgICAgICAgaWYgKGEgIT0gMCAmJiBiICE9IDAgJiYgYyA9PSAwKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigyKTsKICAgICAgICAgICAgaWYgKC1iIC8gYSA+IDApIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgWkVSTyk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgLWIgLyBhKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQuZm9ybWF0KEZPUk1BVCwgLWIgLyBhKTsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigpOwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5mb3JtYXQoRk9STUFULCBaRVJPKTsKICAgICAgICAgICAgfQogCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAKICAgICAgICAvLyAxMTEKICAgICAgICAvLyBheF4yICsgYnggKyBjID0gMAogICAgICAgIGlmIChhICE9IDAgJiYgYiAhPSAwICYmIGMgIT0gMCkgewogICAgICAgICAgICBkb3VibGUgZCA9IGIgKiBiIC0gNCAqIGEgKiBjOwogCiAgICAgICAgICAgIGlmIChkIDwgMCkgewogICAgICAgICAgICAgICAgLy8gbm8gcmVhbCByb290cwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKDApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKGQgPiAwKXsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigyKTsKICAgICAgICAgICAgICAgIGRvdWJsZSBhbnMxID0gKC1iIC0gTWF0aC5zcXJ0KGQpKSAvICgyICogYSk7CiAgICAgICAgICAgICAgICBkb3VibGUgYW5zMiA9ICgtYiArIE1hdGguc3FydChkKSkgLyAoMiAqIGEpOwogICAgICAgICAgICAgICAgaWYgKGFuczIgPiBhbnMxKSB7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5mb3JtYXQoRk9STUFULCBhbnMxKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LmZvcm1hdChGT1JNQVQsIGFuczIpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5mb3JtYXQoRk9STUFULCBhbnMyKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LmZvcm1hdChGT1JNQVQsIGFuczEpOwogICAgICAgICAgICAgICAgfQogCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oMSk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LmZvcm1hdChGT1JNQVQsICgtYiAvICgyICogYSkpKTsKICAgICAgICAgICAgfQogCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAKIAogCiAgICB9CiAKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHJlYWRlciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgZG91YmxlIGEgPSByZWFkZXIubmV4dEludCgpOwogICAgICAgIGRvdWJsZSBiID0gcmVhZGVyLm5leHRJbnQoKTsKICAgICAgICBkb3VibGUgYyA9IHJlYWRlci5uZXh0SW50KCk7CiAKICAgICAgICBNYWluIHRhc2sgPSBuZXcgTWFpbigpOwogICAgICAgIHRhc2suc29sdmUoYSwgYiwgYyk7CiAgICB9Cn0=