Skip to content

Instantly share code, notes, and snippets.

@yucao24hours
Last active December 20, 2015 09:49
Show Gist options
  • Save yucao24hours/6110489 to your computer and use it in GitHub Desktop.
Save yucao24hours/6110489 to your computer and use it in GitHub Desktop.
入力されたふたつの年のあいだにある閏年を表示するプログラムです。
puts "Type the start year."
start_year = gets.chomp.to_i
puts "Then, type the end year."
end_year = gets.chomp.to_i
puts "=================================="
puts "The followings are the leap years."
puts "=================================="
i = start_year
while i <= end_year
if (i % 400) == 0
# 400で割り切れればうるう年
puts i
elsif i % 100 != 0
# 400では割り切れない場合
if i % 4 == 0
# 100で割り切れず、4では割り切れればうるう年
puts i
end
end
i = i + 1
end
puts "=================================="
@do-aki
Copy link

do-aki commented Aug 2, 2013

範囲に対して処理していることが明確になるように Range を使うとループがすっきりするかな。

(start_year..end_year).each do |y| 
   puts y if (y % 400) == 0 || ((y % 100) != 0 && (y % 4) == 0)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment