Created
April 7, 2021 10:00
-
-
Save quanon/0d26c85f6b5bf9e5fd227066a4b7a66f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'terminal-table' # https://github.com/tj/terminal-table | |
| |
N = 30 # 合計人数 | |
| |
# table[n][0] は男子を選ぶ組み合わせの数。 | |
# table[n][1] は女子を選ぶ組み合わせの数。 | |
table = Array.new(N + 1) { Array.new(2, 0) } | |
| |
# n = 1 のときは男子も女子も 1 通りずつ。 | |
table[1][0] = 1 | |
table[1][1] = 1 | |
| |
(1...N).each do |n| | |
# 男子の場合は直前が男子でも女子でも OK。 | |
table[n + 1][0] += (table[n][0] + table[n][1]) | |
# 女子の場合は直前が男子の場合のみ。 | |
table[n + 1][1] = table[n][0] | |
end | |
| |
puts( | |
Terminal::Table.new do |t| | |
t.headings = [nil, '男子', '女子'] | |
table.each_with_index { |row, i| t.add_row([i, *row]) } | |
end | |
) | |
puts("答: #{table[N][0] + table[N][1]}") |
Author
quanon
commented
Apr 7, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment