Skip to content

Instantly share code, notes, and snippets.

@coderaven
Created December 8, 2012 09:04
Show Gist options
  • Save coderaven/4239412 to your computer and use it in GitHub Desktop.
Save coderaven/4239412 to your computer and use it in GitHub Desktop.
Solution for Devcon C-Cup Problem A
gets.to_i.times do
q = gets.to_i
sum_x, sum_y, sum_x2, sum_xy = 0, 0, 0, 0
q.times do
x,y=gets.split.map(&:to_f)
sum_x += x
sum_y += y
sum_x2 += x**2
sum_xy += x*y
end
mx = sum_x / q
my = sum_y / q
a = (sum_xy - sum_x * my) / (sum_x2 - sum_x * mx)
b = my - a * mx
a,b = [a,b].map {|p| p.to_i == p ? p.to_i : (p * 100).floor / 100.0}
puts "#{a} #{b}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment