Created
January 28, 2015 02:50
-
-
Save junichiro/c5279f312558d82a1845 to your computer and use it in GitHub Desktop.
code_iq: 1235
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
# -*- coding: utf-8 -*- | |
import random | |
class code_iq: | |
def _moves(self, a, b): | |
u"""1桁の正の整数を2つ与えると動く玉の数を返す""" | |
if a > b: | |
a, b = b, a | |
if (a < 5 and b < 5) or a >= 5: | |
return b - a | |
else: | |
return self.moves((b-5), a) + 1 | |
def moves(self, a, b): | |
u"""2桁の正の整数を2つ与えると動く玉の数を返す""" | |
if a > b: | |
a, b = b, a | |
al = self.num_list(a) | |
bl = self.num_list(b) | |
return self._moves(al[0], bl[0]) + self._moves(al[1], bl[1]) | |
def num_list(self, n): | |
u"""数を桁に分けて返す""" | |
return (n / 10), (n % 10) | |
def sum_moves(self, l): | |
sn = l.pop(0) | |
v0 = sn | |
res = self.moves(0, sn) | |
for v in l: | |
sn = sn + v | |
res = res + self.moves(sn, v0) | |
v0 = sn | |
return res | |
def min_num(self, a, l): | |
res = 100 | |
resv = 0 | |
for v in l: | |
new = self.moves(a, v) | |
if new < res: | |
res = new | |
resv = v | |
l.remove(resv) | |
return (resv, a + resv, l) | |
if __name__ == '__main__': | |
code = code_iq() | |
a, n = 1, 10 | |
_sum = 0 | |
_min = 100 | |
for i in range(0, 50000): | |
l = range(a, n + 1) | |
random.shuffle(l) | |
_sum = code.sum_moves(l) | |
if _sum < _min: | |
_min = _sum | |
print _min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment