LoginSignup
0
0

More than 3 years have passed since last update.

[問題] 連休の天気(Ruby編)

Posted at

[問題] 連休の天気(Ruby編)を解く

Q

あなたは7日間の連休をもらいましたが、降水確率が 30% 以下ならば外に出掛ける事に決めました。

7日間の降水確率(%)が改行区切りで入力されるので、出掛ける日数の合計を出力してください。

13
0
15
31
89
100
30
と与えられた場合
4
と出力するプログラムを作成してください。

入力される値

入力は以下のフォーマットで与えられます。

t_1
t_2
t_3
t_4
t_5
t_6
t_7

期待する出力

7日間の降水確率が改行区切りで入力されるので、出掛ける日数の合計を出力してください。

私の答え

t_1 = gets.chomp.to_i
t_2 = gets.chomp.to_i
t_3 = gets.chomp.to_i
t_4 = gets.chomp.to_i
t_5 = gets.chomp.to_i
t_6 = gets.chomp.to_i
t_7 = gets.chomp.to_i
int = t_1, t_2, t_3, t_4, t_5, t_6, t_7 
str = int.select do |i|
    i <= 30
end
print str.size

解説

今回は t_1 から t_7 までの数値を int= で配列の中に格納しました。こうする事によって、配列の中から特定の値を取り出す事ができるselectメソッドが使えるようになります。なのでprintでstrを出力すると[13,0,15,30]という結果になります。今回はその数を出力する問題なのでsizeメソッドで値の数を出力しました。

以上!

0
0
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
0
0