x2 - 2 |x| + 1なお,変数xの与え方は,
--------------
|x - 2|
x = rand(20) - 10 |
のように実行のたびに変わるものとし,表示結果は例えば次のようになるものとする.
x = 5 (x^2 - 2|x| + 1) / |x - 2| = 5.33 |
また,0による除算は実行不可能なので,その場合には
|x - 2| = 0 Mission impossible. |
とだけ表示する.
x = rand(20) - 10 y = x z = x - 2 if z == 0 z = nil elsif z < 0 z = -z if y < 0 y = -x end elsif y < 0 y = -y z = -z end if z ans = ( x*x - 2*y + 1.0 ) / z printf "x = %d\n(x^2 - 2|x| + 1) / |x - 2| = %1.2f\n", x, ans else printf "|x - 2| = 0\nMission impossible.\n" end |
別解
x = rand(20) - 10 y = x z = x - 2 if z == 0 z = nil elsif z < 0 z = -z end if y < 0 y = -y end if z ans = (x*x - 2*y + 1.0) / z printf "x = %d\n(x^2 - 2|x| + 1) / |x - 2| = %1.2f\n", x, ans else printf "|x - 2| = 0\nMission impossible.\n" end |
Back |