首页 > 其他分享 >HJ89 24点运算 用递归生成器进行数列穷举

HJ89 24点运算 用递归生成器进行数列穷举

时间:2023-05-01 17:11:42浏览次数:52  
标签:24 生成器 operator flag1 new print 穷举

思路:

1、对4张牌进行全排序,并输出列表

2、分别对排序进行计算尝试,采用穷举方式

3、返回结果

除了用递归生成器进行数组全排序外,也用模块from itertools import permutations,进行全排序。

 1 #输出算式运算顺序从左至右运算,不需要括号确定优先级。
 2 
 3 #列举所有排序方式,比如 1234,1324......
 4 def relist(a):#用递归生成器返回不重复顺序的列表
 5     if len(a)==1:
 6         yield a
 7     for i in range(len(a)):
 8         for j in relist(a[:i]+a[i+1:]):
 9             #print( a[i],j)
10             yield [a[i]]+j   

12 def twentyfour(a):#采用多重循环对数组a所有计算方式穷举。
13     operator=['+','-','*','/']
14     for i in operator:
15         for j in operator:
16             for n in operator:
17                 if dop[i](dop[j](dop[n](a[0],a[1]),a[2]),a[3])==24:
18                     return a,n,j,i
19 import operator
20 dop={'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.truediv}
21 d={'J':11,'Q':12,'K':13,'A':1}
22 a=input().split()
23 b=a.copy()
24 flag=0
25 for k,i in enumerate(a):
26     if i.isdigit():
27         a[k]=int(i)
28     elif i in d:
29         a[k]=d[i]
30     else:
31         print("ERROR")
32         flag=1
33 #用flag使当输出”ERROR"不再进行后面运算,用flag1,输出NONE.
34 if flag==0:
35     flag1=0
36     for i in relist(a):
37         #print(i)
38         new=[]
39         if twentyfour(i):
40             temp,n1,n2,n3=twentyfour(i)
41             for i in temp:
42                 for k,j in enumerate(a):
43                     if i==j:                             #查表打印特殊字符JQKA
44                         new.append(b[k])
45                         break
46             tem=str(new[0])+n1+str(new[1])+n2+str(new[2])+n3+str(new[3])
47             print(tem)
48             flag1=1
49             break
50     if flag1==0:
51         print('NONE')

 

标签:24,生成器,operator,flag1,new,print,穷举
From: https://www.cnblogs.com/tanyuanqing/p/17366708.html

相关文章

  • Visual AssistX Version 10.9.2488 Cracked
    任何问题请反馈至邮箱:[email protected](随缘查看邮件)Anyporbs->[email protected]再次声明:本破解补丁仅供交流学习和研究使用,不可用于商业。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。Notice:thispatcherisforcommunication,lear......
  • 上周热点回顾(4.24-4.30)
    热点随笔:· 服务器出现了一个新软件,一帮大佬吵起来了! (轩辕之风)· 【故障公告】被放出的Bing爬虫,又被爬宕机的园子 (博客园团队)· [MAUI]模仿网易云音乐黑胶唱片的交互实现 (林晓lx)· VueHub:我用ChatGPT开发的第一个项目,送给所有Vue爱好者 (DOM哥)· .NETWeb......
  • Visual AssistX Version 10.9.2488 Cracked
    任何问题请反馈至邮箱:[email protected](随缘查看邮件)Anyporbs->[email protected]再次声明:本破解补丁仅供交流学习和研究使用,不可用于商业。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。Notice:thispatcherisforcommunication,lear......
  • 时间转化Fri Apr 07 11:43:24 +0800 2022
    原格式:FriApr0711:43:24+08002022灵感:获取微博某时间段到...之间的内容转化格式:2022-04-07#实现importdatetimedate_str="FriApr0711:43:24+08002022"date=datetime.datetime.strptime(date_str,"%a%b%d%H:%M:%S%z%Y")formatted_date=date.str......
  • CF1624G 题解
    前言题目传送门!更好的阅读体验?比较好玩的二进制题目。思路答案最小,也就是说较高位要尽可能小。所以很容易想到从最高位开始枚举。第\(i\)位为\(0\),等价于选出的所有边的第\(i\)位都为\(0\)。同时,由于我们是贪心,如果之前枚举过的第\(j\)位可以是\(0\),那么这两个条件......
  • springboot mybatis-plus 3.5.1代码生成器配置
    springbootmybatis-plus3.5.1代码生成器配置https://blog.csdn.net/Lean_on_Me/article/details/128066822  ......
  • CSC246/446机器学习
    CSC246/446MachineLearningHomework5:ClusteringObjectiveYouaregiventwokindsofdataset.DatasetsA,B,C,andZwerecreatedbytheinstructorbysamplingfromfourdifferentGaussianmixtures,withdifferentnumbersofmixturecomponents.Yourfi......
  • COMP30024人工智能
    ProjectPartBPlayingtheGameCOMP30024ArtificialIntelligenceApril20231OverviewInthissecondpartoftheproject,wewillplaythefulltwo-playerversionofInfexion.Beforeyoureadthisspecificationyoushouldre-readthe‘RulesfortheGame......
  • #10024. 「一本通 1.3 练习 3」质数方阵
    loj题目传送门一本通题目传送门洛谷传送门原题是UVA835,是多测思路肯定是要剪枝的呀众所周知,dfs的路径像树一样显而易见,树的某一层的节点越少,他的下面的分支就越少于是我们考虑改变搜索顺序来剪掉更多的分支一个数的末位要是\(0\),那他肯定不是质数。于是我们先从所有数的......
  • ABC240Ex
    给定长为\(n\)的01字符串\(s\),求一个最大的\(k\),使得能选出\(k\)个形如\([l_i,r_i]\)的区间,满足:\(\foralli\in[2,k],l_i\gtr_{i-1}\).\(\foralli\in[2,k]\),\(s_{l_i\simr_i}\)的字典序严格大于\(s_{l_{i-1}\simr_{i-1}}\).\(1\len\le2.5\tim......