fork download
  1. import java.io.PrintStream;
  2. import java.text.DecimalFormat;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created on 07/10/14.
  7.  * Ax2 + Bx + C = 0.
  8.  Your task is to find the number of distinct roots of the equation and print all of them in ascending order.
  9.  
  10.  Input
  11.  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.
  12.  
  13.  Output
  14.  In case of infinite root count print the only integer -1. In case of no roots print the only integer 0.
  15.  In other cases print the number of root on the first line and the roots on the following lines in the ascending order.
  16.  Print roots with at least 5 digits after the decimal point.
  17.  */
  18. public class Main {
  19.  
  20. public static final String FORMAT = "%.5f";
  21. public static final double ZERO = 0;
  22.  
  23.  
  24. private void solve(double a, double b, double c) {
  25. // consider 8 cases
  26.  
  27. // 000
  28. // infinite # of roots
  29. if (a == 0 && b == 0 && c == 0) {
  30. System.out.println(-1);
  31. return;
  32. }
  33.  
  34. // 001
  35. // equation: c = 0 where c != 0 doesn't have roots
  36. if (a == 0 && b == 0 && c != 0) {
  37. System.out.println(0);
  38. return;
  39. }
  40.  
  41. // 010
  42. // bx = 0, one zero root
  43. if (a == 0 && b != 0 && c == 0) {
  44. System.out.println(1);
  45. System.out.format(FORMAT, ZERO);
  46. return;
  47. }
  48.  
  49. // 011
  50. // bx + c = 0
  51. // one root
  52. if (a == 0 && b != 0 && c != 0) {
  53. System.out.println(1);
  54. System.out.format(FORMAT, -c / b);
  55. return;
  56. }
  57.  
  58. // 100
  59. // ax^2 = 0
  60. // one distinct root
  61. if (a != 0 && b == 0 && c == 0) {
  62. System.out.println(1);
  63. System.out.format(FORMAT, ZERO);
  64. return;
  65. }
  66.  
  67. // 101
  68. // ax^2 + c = 0
  69. if (a != 0 && b == 0 && c != 0) {
  70. if (a * c < 0) {
  71. System.out.println(2);
  72. System.out.format(FORMAT, - Math.sqrt(-c / a));
  73. System.out.println();
  74. System.out.format(FORMAT, Math.sqrt(-c / a));
  75. }
  76. else {
  77. System.out.println(0);
  78. }
  79. return;
  80. }
  81.  
  82. // 110
  83. // ax^2 + bx = 0
  84. if (a != 0 && b != 0 && c == 0) {
  85. System.out.println(2);
  86. if (-b / a > 0) {
  87. System.out.format(FORMAT, ZERO);
  88. System.out.println();
  89. System.out.format(FORMAT, -b / a);
  90. }
  91. else {
  92. System.out.format(FORMAT, -b / a);
  93. System.out.println();
  94. System.out.format(FORMAT, ZERO);
  95. }
  96.  
  97. return;
  98. }
  99.  
  100. // 111
  101. // ax^2 + bx + c = 0
  102. if (a != 0 && b != 0 && c != 0) {
  103. double d = b * b - 4 * a * c;
  104.  
  105. if (d < 0) {
  106. // no real roots
  107. System.out.println(0);
  108. }
  109. else if (d > 0){
  110. System.out.println(2);
  111. double ans1 = (-b - Math.sqrt(d)) / (2 * a);
  112. double ans2 = (-b + Math.sqrt(d)) / (2 * a);
  113. if (ans2 > ans1) {
  114. System.out.format(FORMAT, ans1);
  115. System.out.println();
  116. System.out.format(FORMAT, ans2);
  117. }
  118. else {
  119. System.out.format(FORMAT, ans2);
  120. System.out.println();
  121. System.out.format(FORMAT, ans1);
  122. }
  123.  
  124. }
  125. else {
  126. System.out.println(1);
  127. System.out.format(FORMAT, (-b / (2 * a)));
  128. }
  129.  
  130. return;
  131. }
  132.  
  133.  
  134.  
  135. }
  136.  
  137. public static void main(String[] args) {
  138. Scanner reader = new Scanner(System.in);
  139. double a = reader.nextInt();
  140. double b = reader.nextInt();
  141. double c = reader.nextInt();
  142.  
  143. Main task = new Main();
  144. task.solve(a, b, c);
  145. }
  146. }
Success #stdin #stdout 0.12s 56792KB
stdin
1 -5 6
stdout
2
2.00000
3.00000