Skip to content

Instantly share code, notes, and snippets.

@shoyan
Last active December 22, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyan/6401809 to your computer and use it in GitHub Desktop.
Save shoyan/6401809 to your computer and use it in GitHub Desktop.
3つの配列から同じ数を見つけるアルゴリズムをRubyで実装してみた。
# encoding: utf-8
#
# 3つの配列から同じ数を見つける
#
# 同じ数 31
list1 = [2, 6, 10, 16, 24, 31, 38, 40, 50]
list2 = [1, 7, 11, 17, 25, 31, 39, 41, 51]
list3 = [3, 9, 13, 19, 27, 31, 32, 42, 52]
i = 0
j = 0
k = 0
# Rubyの最大値の求め方どうするんだろ?
max = 1073741823
list1.push(max)
list2.push(max)
list3.push(max)
while (list1[i] != list2[j] || list2[j] != list3[k]) do
if (list1[i] < list2[j]) then
i += 1
elsif (list2[j] < list3[k]) then
j += 1
else
k += 1
end
end
if (list1[i] == max) then
puts "一致する数はありませんでした"
else
puts "一致する数は#{list1[i]}です。"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment