LoginSignup
1
1

More than 3 years have passed since last update.

ランダムに引いた4つの数字を、2つの二桁の数字にして、和を最大にする【ruby初心者】

Posted at

はじめに

適当に引いた1桁の数字を、2枚ペアに分けて、2桁の数字にし和を最大化する。

入力部分

input_line = gets.split(" ").map(&:to_i)
input_line = input_line.sort
#=> 配列が

計算部分

1〜2番目に大きい数字は、2桁目。
後の2つは1桁目になる。

a = input_line[3]*10 + input_line[1]
b = input_line[2]*10 + input_line[0]

p a + b

ちなみに、上の3行を1行でも書ける。
その場合こんな感じ

p input_line[3]*10 + input_line[2]*10 + input_line[1] + input_line[0] 
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1