一道小学算术题,但是好多老师不会,不知道为什么
解题链接: http://ctf5.shiyanbar.com/program/2/
import os
pos = []
in_m = []
out_m = []
in_pos = []
out_pos = []
cnt = 0
ans = 0
def cal_ans(a,b):
if b==10:
return a*100+b
else:
return a*10+b
def check():
global in_m
global out_m
global in_pos
global out_pos
global ans
m = []
for i in range(10):
m.append(0)
for i in range(5):
m[in_pos[i]] = in_m[i]
m[out_pos[i]] = out_m[i]
#print m
sum_check = []
for i in range(0,10,2):
sum_check.append(m[i]+m[i+1]+m[(i+2)%10])
flag = 1
for i in range(1,5):
if sum_check[i]!=sum_check[i-1]:
flag = 0
break
if flag == 1:
#print m
tmp = 0
m_min = 10000000000000000
m_t = 1
for i in range(1,10,2):
if m[i] < m_min:
m_min = m[i]
m_t = i
for i in range(5):
tmp = cal_ans(tmp , m[m_t])
tmp = cal_ans(tmp , m[m_t-1])
tmp = cal_ans(tmp , m[(m_t+1)%10])
m_t = (m_t + 2)%10
#print tmp
if tmp > ans:
ans = tmp
def array_out(dep):
global out_pos
if dep==5:
check()
return
for i in range(1,10,2):
if i not in out_pos:
out_pos.append(i)
array_out(dep+1)
out_pos.remove(i)
def array_in(dep):
global in_pos
if dep==5:
array_out(0)
return
for i in range(0,10,2):
if i not in in_pos:
in_pos.append(i)
array_in(dep+1)
in_pos.remove(i)
def conbination(n,summ,dep):
global in_m
global out_m
global cnt
if dep==5:
if summ%5==0:
out_m = []
for i in range(1,11):
if i not in in_m:
out_m.append(i)
array_in(0)
#for i in range(5):
# print m[i],
#print "\n"
#inter_cycle.append(m)
cnt += 1
return
for i in range(n+1,10):
in_m.append(i)
conbination(i,summ+i,dep+1)
in_m.remove(i)
if __name__ == '__main__':
conbination(0,0,0)
print cnt
print ans