pontz_rwのブログ

プログラミング等の備忘録

Volume0: 0004

連立方程式 | Aizu Online Judge

連立方程式の解を求める問題です

クラーメルの公式を利用します

Python

# coding: utf-8

while True:
    try:
        a, b, c, d, e, f = [int(i) for i in input().split()]

        detA = a * e - b * d
        detA1 = c * e - b * f
        detA2 = a * f - c * d

        # -0.000 を 0.000 にするために 0 を足す
        print('{:.3f} {:.3f}'.format(detA1 / detA + 0, detA2 / detA + 0))
    except EOFError:
        break

Ruby

# coding: utf-8

while line = gets
    a, b, c, d, e, f = line.strip.split.map(&:to_f)
    detA = a * e - b * d
    detA1 = c * e - b * f
    detA2 = a * f - c * d
    # -0.000 を 0.000 にするために 0 を足す
    puts "%.3f %.3f" % [detA1 / detA + 0, detA2 / detA + 0]
end

文字列の%演算は、第1引数にフォーマット、第2引数にフォーマットしたい値を指定します

書式に埋め込む値が複数ある場合は、右辺を配列にします

"%.3f %.3f" % [detA1 / detA + 0, detA2 / detA + 0]