也当学过了python先
时间:2010-07-13 来源:beyondmind
用python解了个题【火柴游戏(250分)】,题目在这http://www.bianchengla.com/team/81/contest/97/problem/A ,当作入门了,呵呵。
# -*- coding: GBK -*- exchange_data = [ #索引位的数可移动1根而变为其他数,2代表不能做到,0,1,-1代表添加1根或者减少1根或者自身移动 #0==> #0,9,6,8 [2,2,2,2,2,2,0,2,1,0], #1==> #1,7 [2,2,2,2,2,2,2,1,2,2], #2==> #2,3 [2,2,2,0,2,2,2,2,2,2], #3==> #2,3,9,5 [2,2,0,2,2,0,2,2,2,1], #4==> #4 [2,2,2,2,2,2,2,2,2,2], #5==> #3,5,6,9 [2,2,2,0,2,2,1,2,2,1], #6==> #0,5,6,8,9 [2,2,2,2,2,-1,2,2,1,0], #7==> #1,7 [2,-1,2,2,2,2,2,0,2,2], #8==> #0,6,8,9 [-1,2,2,2,2,2,-1,2,2,-1], #9==> #0,3,5,6,8,9 [0,2,2,-1,2,-1,0,2,1,2] ] has_result = False def func(num1, op, num2, num3): tmp1 = exchange_data[num1] tmp2 = exchange_data[num2] tmp3 = exchange_data[num3] for i in range(0, 10): if(tmp1[i]>=2 and i!=num1):continue; for j in range(0, 10): if(tmp2[j]>=2 and j!=num2):continue; for k in range(0, 10): if(tmp3[k]>=2 and k!=num3):continue; if(op=='+' and i+j==k and is_result(tmp1[i], tmp2[j], tmp3[k])): print i, op, j, '=', k if(op=='+' and i-j==k and is_result(tmp1[i], tmp2[j], tmp3[k], -1)): print i, '-', j, '=', k if(op=='-' and i-j==k and is_result(tmp1[i], tmp2[j], tmp3[k])): print i, op, j, '=', k if(op=='-' and i+j==k and is_result(tmp1[i], tmp2[j], tmp3[k], 1)): print i, '+', j, '=', k def is_result(*result): global has_result moveNum = 0 total = 0 for i in result: if(i<2): moveNum+=1 total+=i if(i==0): moveNum+=1 if(total!=0 or moveNum!=2): return False has_result = True return True def parse0(arg): #print arg[0], arg[1], arg[2], arg[4] global has_result has_result = False func(int(arg[0]), arg[1],int(arg[2]),int(arg[4])) if has_result==False: print -1; if(__name__=="__main__"): import sys parse0(sys.argv[1])
D:\Runtime\python27>python problem1.py 9+5=9 3 + 5 = 8 3 + 6 = 9 D:\Runtime\python27>python problem1.py 9+4=9 -1 D:\Runtime\python27>python problem1.py 9+0=9 -1 D:\Runtime\python27>python problem1.py 9+1=9 9 - 1 = 8
相关阅读 更多 +