This documentation is automatically generated by online-judge-tools/verification-helper

:heavy_check_mark: test/math/quadratic_equation_test.cr

Depends on

Code

# verification-helper: PROBLEM https://yukicoder.me/problems/no/955
# verification-helper: ERROR 1e-11
require "../../src/math/quadratic_equation"
a, b, c = read_line.split.map(&.to_i64)
ans = Math.quadratic_equation(a, b, c)
if ans.nil?
  puts -1
else
  puts ans.size, ans.join('\n')
end
# verification-helper: PROBLEM https://yukicoder.me/problems/no/955
# verification-helper: ERROR 1e-11
# require "../../src/math/quadratic_equation"
module Math
  # Solve `ax^2 + bx + c = 0`.
  def quadratic_equation(a : Int64, b : Int64, c : Int64) : Array(Float64)?
    if a == 0 && b == 0 && c == 0
      nil
    elsif a == 0 && b == 0
      [] of Float64
    elsif a == 0
      [-(c / b)]
    elsif (d = b * b - a * c * 4) < 0
      [] of Float64
    elsif d == 0
      [-b / (a * 2)]
    else
      x1 = (b > 0) ? (-b - Math.sqrt(d)) / (a * 2) : (-b + Math.sqrt(d)) / (a * 2)
      x2 = (c / a) / x1
      [{x1, x2}.min, {x1, x2}.max]
    end
  end
end

a, b, c = read_line.split.map(&.to_i64)
ans = Math.quadratic_equation(a, b, c)
if ans.nil?
  puts -1
else
  puts ans.size, ans.join('\n')
end
Back to top page