Skip to content

Instantly share code, notes, and snippets.

Created April 2, 2013 05:17
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 anonymous/5290100 to your computer and use it in GitHub Desktop.
Save anonymous/5290100 to your computer and use it in GitHub Desktop.
#coding: utf-8
require 'socket'
def solve(cnt, the_end)
pattern = {111 => [471], 333 => [477], 777 => [753], 888 => [192, 442, 692, 942], 999 => [999]}
a = (cnt-1) / pattern[the_end].length
b = pattern[the_end][cnt % pattern[the_end].length - 1]
return a.to_s + b.to_s
end
# puts solve(265613988875874769338781322035779626829233452653394495974574961739092490901302182994384699044001, 888)
def get_problem(s)
line = ""
while true
c = s.read(1)
return line if c == nil
line += c
if c == "?"
return line
end
end
end
#main
s = TCPSocket.open("nullcon-e1.no-ip.org", 2000)
p s.gets; p s.gets; p s.gets;
loop do
p line = get_problem(s)
if line =~ /What is the (\d+)(?:th|rd|st|nd) positive integer whose cube ends with the digits (\d+)?/
cnt = $1
the_end = $2
puts ans = solve(cnt.to_i, the_end.to_i).to_s
s.puts ans
else
p line
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment