Created
February 13, 2017 16:24
-
-
Save mnmkng/2d878679d4624a74d3df0e35ab4f7464 to your computer and use it in GitHub Desktop.
trojuhelniky
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 'set' | |
arr = [3,6,6,3,1,7,20,9,11,3,1,8,2,2] | |
# nejdriv najdu vsechny trojice v poli | |
triples = [] | |
i = 0 | |
j = 1 | |
k = 2 | |
while i < (arr.length-2) | |
while j < (arr.length-1) | |
while k <(arr.length) | |
triples << [arr[i],arr[j],arr[k]] | |
k += 1 | |
end | |
j += 1 | |
k = j+1 | |
end | |
i += 1 | |
j = i+1 | |
k = i+2 | |
end | |
# mnozinou se zbavim duplicit | |
clean = Set.new(triples) | |
# zjistim z ceho se vubec da sestavit trojuhelnik | |
triangles = [] | |
clean.each do |item| | |
if ((item[0] + item[1]) > item[2]) and ((item[1] + item[2]) > item[0]) and ((item[0] + item[2]) > item[1]) | |
triangles << item | |
end | |
end | |
# najdu trojuhelnik s nejmensim obvodem tak, ze si za minimum dosadim prvni trojuhelnik z vyctu | |
min = triangles[0][0] + triangles[0][1] + triangles[0][1] | |
min_triangle = triangles[0] | |
triangles.each do |item| | |
circumference = item[0] + item[1] + item[2] | |
if circumference < min | |
min = circumference | |
min_triangle = item | |
end | |
end | |
puts "Trojuhelnik s nejmensim obvodem #{min} je #{min_triangle} " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment