LoginSignup
1
0

More than 1 year has passed since last update.

最大値を配列を使わずに標準出力する方法、配列を使う方法

Posted at

最大値を配列を使わずに標準出力する方法、配列を使って標準出力に出力する方法の両方を投稿します。

配列を使わない方法

N = gets.to_i
max_num =0
for i in (1..N).each do
    num = gets.to_i
    if num >= max_num
        max_num = num
    end
end    
puts max_num

配列を使う方法

n = gets.to_i

arr = Array.new(n)
n.times { |i| arr[i] = gets.to_i }

puts arr.max

配列を使う方がシンプルにコーディングできますね。

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