题解 | #24点运算#
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
s =input().split() dic=['J','Q','K','A'] value=[11,12,13,1] inp=[] if 'joker'in s or 'JOKER' in s: print('ERROR') else: for idx,v in enumerate(s): if v in dic: inp.append(value[dic.index(v)]) else: inp.append(v) operate=[] def func(sr,idx,res,num,cz): if num==3 and res==24: global operate operate.append(cz) return True if idx<3: func(sr,idx+1,res+int(sr[idx+1]),num+1,cz+['+']) if idx<3: func(sr,idx+1,res-int(sr[idx+1]),num+1,cz+['-']) if idx<3: func(sr,idx+1,res*int(sr[idx+1]),num+1,cz+['*']) if idx<3: func(sr,idx+1,res//int(sr[idx+1]),num+1,cz+['/']) res1=[] def qpx(s,l,res): if l==0: global res1 res1.append(res) return res if l>0: for i in range(l): qpx(s[0:i]+s[i+1:l],l-1,res+[s[i]]) qpx(inp,4,[]) p=[] for i in res1: func(i,0,int(i[0]),0,[]) if len(operate)>0: j = 0 for sz in i: if sz in value: p.append(dic[value.index(sz)]) else: p.append(sz) if j<len(operate[0]): p.append(operate[0][j]) j+=1 break if p: print(''.join(p)) else: print('NONE')