Skip to content

Instantly share code, notes, and snippets.

@dukeimg
Created April 22, 2016 03:38
Show Gist options
  • Save dukeimg/29264f14fdc18966de4140b7f41a2616 to your computer and use it in GitHub Desktop.
Save dukeimg/29264f14fdc18966de4140b7f41a2616 to your computer and use it in GitHub Desktop.
Temperatures
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
@n = gets.to_i # the number of temperatures to analyse
@temps = gets.chomp # the n temperatures expressed as integers ranging from -273 to 5526
if @n == 0
puts 0
return
end
# Write an action using puts
# To debug: STDERR.puts "Debug messages..."
closest = 5526
arr = @temps.split(" ").map { |s| s.to_i }
arr.each do |t|
if t.abs < closest.abs
closest = t
elsif t.abs == closest.abs && t > 0
closest = t
end
end
puts closest
# А можно так
# if @n == 0
# puts 0
# else
# puts arr.min_by { |t| [t.abs, -t] }
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment