也当学过了python先

用python解了个题【火柴游戏(250分)】,题目在这[url]http://www.bianchengla.com/team/81/contest/97/problem/A[/url] ,当作入门了,呵呵。

# -*- 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值