也当学过了python先
用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
,
#1==>
#1,7
,
#2==>
#2,3
,
#3==>
#2,3,9,5
,
#4==>
#4
,
#5==>
#3,5,6,9
,
#6==>
#0,5,6,8,9
,
#7==>
#1,7
,
#8==>
#0,6,8,9
[-1,2,2,2,2,2,-1,2,2,-1],
#9==>
#0,3,5,6,8,9
]
has_result = False
def func(num1, op, num2, num3):
tmp1 = exchange_data
tmp2 = exchange_data
tmp3 = exchange_data
for i in range(0, 10):
if(tmp1>=2 and i!=num1):continue;
for j in range(0, 10):
if(tmp2>=2 and j!=num2):continue;
for k in range(0, 10):
if(tmp3>=2 and k!=num3):continue;
if(op=='+' and i+j==k and is_result(tmp1, tmp2, tmp3)):
print i, op, j, '=', k
if(op=='+' and i-j==k and is_result(tmp1, tmp2, tmp3, -1)):
print i, '-', j, '=', k
if(op=='-' and i-j==k and is_result(tmp1, tmp2, tmp3)):
print i, op, j, '=', k
if(op=='-' and i+j==k and is_result(tmp1, tmp2, tmp3, 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, arg, arg, arg
global has_result
has_result = False
func(int(arg), arg,int(arg),int(arg))
if has_result==False:
print -1;
if(__name__=="__main__"):
import sys
parse0(sys.argv)
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
页:
[1]