1、递归求和

1 def sum(n):
2    if n >0:
3       return n+sum(n-1)
4    else:
5       return 0
6 print(sum(5))

递归求和

2、昆特牌最大化战力

 1 #困特牌决斗算法
 2 def duel(first,last):
 3    D_value = first - last
 4    if D_value>=0:
 5       return first
 6    else:
 7       return duel(abs(D_value),first)
 8 #计算决斗赚了几点战力
 9 def cha(first,last):
10    return first+last-duel(first,last)
11 #传入列表,找出其中最赚的一组
12 def max_xx(l):
13    duel_team={}
14    for i in l:
15       for j in l:
16          if i==j:
17             continue
18          else:
19             duel_team.update({(i,j):cha(i,j)})
20    max_num = max(duel_team.values())
21    duel_list= [k for k,v in duel_team.items() if v==max_num]
22    return {duel_list[0]:duel_team[duel_list[0]]}
23
24 print(max_xx([3,4,6,7,8,9,10,11,12,13,14,15]))

View Code

 1 #困特牌决斗算法,带护甲
 2 first = (8,6)
 3 last = (6,10)
 4 #
 5 def duel_armor(first_num,first_armor,last_num,last_armor):
 6     print(first_num,first_armor,last_num,last_armor)
 7     if first_num >= last_armor:
 8         cha1 = first_num - last_armor
 9         last = last_num - cha1
10         last_num = last
11         last_armor = 0
12         if last<=0:
13             return last_num
14         return duel_armor(last_num,last_armor,first_num,first_armor)
15     else:
16         cha2 = last_armor - first_num
17         last_armor = cha2
18         return duel_armor(last_num, last_armor, first_num, first_armor)
19
20 #计算决斗赚了几点战力
21 def cha(first,last):
22     first_num = first[0]
23     first_armor = first[1]
24     last_num = last[0]
25     last_armor = last[1]
26     return first_num+last_num-duel_armor(first_num,first_armor,last_num,last_armor)
27
28 print(first,last)

带护甲的版本

3、斐波那契数列

 1 #斐波那契数列
 2 def feb(n):
 3    if n == 1 or n == 2:
 4       return 1
 5    else:
 6       return feb(n-1)+feb(n-2)
 7 l=[]
 8 for i in range(1,21):
 9    l.append(feb(i))
10 print(l)

View Code

4、查找文件下所有的路径

1 import os
2 def print_directory_contents(sPath):
3     for child in os.listdir(sPath):
4         dir = os.path.join(sPath,child)
5         if os.path.isdir(dir):
6             print_directory_contents(dir)
7         else:
8             print(dir)
9 print_directory_contents('py')

View Code

5、生成评论树

 1 data = [
 2     ('a',None),
 3     ('b', 'a'),
 4     ('c', None),
 5     ('d', 'a'),
 6     ('e', 'a'),
 7     ('g', 'b'),
 8     ('h', 'g'),
 9     ('j', None),
10     ('f', 'j'),
11 ]
12
13
14
15 def build_tree(data):
16     data_dict = {}
17     for d in data:
18         add_node(data_dict,d)
19     return data_dict
20
21 def add_node(dict,d):
22     if d[1] is None:
23         dict[d[0]] = {}
24     else:
25         for k,v in dict.items():
26             if d[1] == k:
27                 dict[d[1]][d[0]] = {}
28             else:
29                 add_node(v,d)
30
31 print(build_tree(data))

View Code

6、灵山奇缘装备共鸣最优连法(py实现)

 1 #coding:utf8
 2 import itertools
 3 #1、红色(功法) 2、黄色(速度) 3、蓝色(气血) 4、绿色(防御)
 4 #代表全部9件装备
 5 l = [
 6      [2,3,2,1],
 7      [1,1,2,2],
 8      [4,4,1,1],
 9      [2,4,2,1],
10      [4,4,1,2],
11      [3,3,1,4],
12      [2,2,3,4],
13      [1,4,2,3],
14      [1,4,4,2]
15 ]
16 all_l=[]
17 #全排序
18 n = itertools.permutations(l,9)
19 for i in n:
20     all_l.append(i)
21
22 for l2 in all_l:
23     l1 = list(l2)
24     count = 0
25     zuhe=[]
26     if l1[0][0]==l1[1][2]:
27         count+=1
28         zuhe.append(l1[0][0])
29     if l1[0][1]==l1[2][3]:
30         count+=1
31         zuhe.append(l1[0][1])
32     if l1[0][2]==l1[3][0]:
33         count+=1
34         zuhe.append(l1[0][2])
35     if l1[0][3]==l1[3][1]:
36         count+=1
37         zuhe.append(l1[0][3])
38     if l1[5][3]==l1[1][1]:
39         count+=1
40         zuhe.append(l1[5][3])
41     if l1[5][2]==l1[2][0]:
42         count+=1
43         zuhe.append(l1[5][2])
44     if l1[6][0]==l1[4][2]:
45         count+=1
46         zuhe.append(l1[6][0])
47     if l1[6][1]==l1[3][3]:
48         count+=1
49         zuhe.append(l1[6][1])
50     if l1[7][1]==l1[1][3]:
51         count+=1
52         zuhe.append(l1[7][1])
53     if l1[7][2]==l1[4][0]:
54         count+=1
55         zuhe.append(l1[7][2])
56     if l1[8][0]==l1[2][2]:
57         count+=1
58         zuhe.append(l1[8][0])
59     if l1[8][3]==l1[3][1]:
60         count+=1
61         zuhe.append(l1[8][3])
62     l1.append(count)
63     shuxing=[]
64     shuxing.append('攻击(红色)%s条'%zuhe.count(1))
65     shuxing.append('速度(黄色)%s条'%zuhe.count(2))
66     shuxing.append('气血(蓝色)%s条'%zuhe.count(3))
67     shuxing.append('防御(绿色)%s条'%zuhe.count(4))
68     l1.append(shuxing)
69     if count==11:
70         print(l1)

View Code

7、灵山奇缘装备共鸣最优连法(JS实现)

  1 <html>
  2 <head>
  3 <meta charset="utf-8">
  4 <title>计算</title>
  5 </head>
  6 <body>
  7 <p>1、红色(功法) 2、黄色(速度) 3、蓝色(气血) 4、绿色(防御)</p>
  8 <p>等级:
  9     <select>
 10         <option>50-54级</option>
 11         <option>55-59级</option>
 12         <option>61-64级</option>
 13         <option>65级以上</option>
 14     </select>
 15 </p>
 16 <div>
 17     <table border="1" id="buwei_table">
 18         <tr>
 19             <th>部位</th>
 20             <th>是否</th>
 21             <th>左上</th>
 22             <th>右上</th>
 23             <th>右下</th>
 24             <th>左下</th>
 25         </tr>
 26     </table>
 27 </div>
 28
 29 连线条数:<select id="lianxian">
 30     <option>1</option>
 31     <option>2</option>
 32     <option>3</option>
 33     <option>4</option>
 34     <option>5</option>
 35     <option>6</option>
 36     <option>7</option>
 37     <option>8</option>
 38     <option>9</option>
 39     <option>10</option>
 40     <option>11</option>
 41     <option>12</option>
 42 </select>
 43 <input type="button" value="计算" οnclick="FResult()">
 44 <div>
 45     <table border="1">
 46         <tr>
 47             <td width="20" height="20"></td>
 48             <td width="20" height="20"></td>
 49             <td id="tu_td6" width="20" height="20" style="text-align:center;background-color: darkgray">6</td>
 50             <td width="20" height="20"></td>
 51             <td width="20" height="20"></td>
 52         </tr>
 53         <tr>
 54             <td width="20" height="20"></td>
 55             <td id="tu_td2" width="20" height="20" style="text-align:center;background-color: darkgray">2</td>
 56             <td width="20" height="20"></td>
 57             <td id="tu_td3" width="20" height="20" style="text-align:center;background-color: darkgray">3</td>
 58             <td width="20" height="20"></td>
 59         </tr>
 60         <tr>
 61             <td id="tu_td8" width="20" height="20" style="text-align:center;background-color: darkgray">8</td>
 62             <td width="20" height="20"></td>
 63             <td id="tu_td1" width="20" height="20" style="text-align:center;background-color: darkgray">1</td>
 64             <td width="20" height="20"></td>
 65             <td id="tu_td9" width="20" height="20" style="text-align:center;background-color: darkgray">9</td>
 66         </tr>
 67         <tr>
 68             <td width="20" height="20"></td>
 69             <td id="tu_td5" width="20" height="20" style="text-align:center;background-color: darkgray">5</td>
 70             <td width="20" height="20"></td>
 71             <td id="tu_td4" width="20" height="20" style="text-align:center;background-color: darkgray">4</td>
 72             <td width="20" height="20"></td>
 73         </tr>
 74         <tr>
 75             <td width="20" height="20"></td>
 76             <td width="20" height="20"></td>
 77             <td id="tu_td7" width="20" height="20" style="text-align:center;background-color: darkgray">7</td>
 78             <td width="20" height="20"></td>
 79             <td width="20" height="20"></td>
 80         </tr>
 81     </table>
 82 </div>
 83 <div id="result">
 84     <table border="1" id="table">
 85         <tr>
 86             <th>1号位</th>
 87             <th>2号位</th>
 88             <th>3号位</th>
 89             <th>4号位</th>
 90             <th>5号位</th>
 91             <th>6号位</th>
 92             <th>7号位</th>
 93             <th>8号位</th>
 94             <th>9号位</th>
 95             <th>属性</th>
 96             <th>操作</th>
 97         </tr>
 98
 99     </table>
100 </div>
101 <script type="application/javascript">
102     //创建部位列表
103     window.οnlοad=function () {
104         var buwei_arr = ['束带','上衣','护腕','下摆','武器','玉佩','项链','腰带','鞋子'];
105         var buwei_table = document.getElementById('buwei_table');
106         buwei_arr.forEach(function (buwei) {
107              var buwei_tr = document.createElement('tr');
108              var buwei_td = document.createElement('td');
109              buwei_td.innerText=buwei;
110              buwei_tr.appendChild(buwei_td);
111              var shifou_td = document.createElement('td');
112              shifou_td.innerHTML='<select><option value="0">否</option><option value="1">是</option></select>';
113              buwei_tr.appendChild(shifou_td);
114              for (var i=0;i<4;i++){
115                  var td1 = document.createElement('td');
116                  td1.innerHTML='<select><option value="1" style="color:red;">红</option><option value="2" style="color:yellow">黄</option>' +
117                      '<option value="3" style="color:blue">蓝</option><option value="4" style="color:green">绿</option></select>';
118                  buwei_tr.appendChild(td1);
119              };
120              buwei_table.appendChild(buwei_tr);
121         });
122     };
123     function create_arr() {
124         var arr1=[];
125         var buwei_arr = ['束带','上衣','护腕','下摆','武器','玉佩','项链','腰带','鞋子'];
126         var buwei_table = document.getElementById('buwei_table');
127         for (var i=1;i<buwei_table.children.length;i++){
128             var arr2=[];
129             for (var j=2;j<6;j++){
130                 if(buwei_table.children[i].children[1].children[0].selectedIndex===1){
131                     arr2.push(buwei_table.children[i].children[j].children[0].selectedIndex+1)
132                 }
133             }
134             arr2.push(buwei_arr[i-1]);
135             arr1.push(arr2)
136         }
137         return arr1;
138     }
139     //全排列
140     function fullSort(arr){
141         var result = [];
142         if (arr.length == 1) {
143             result.push(arr);
144             return result;
145         }
146         for (var i = 0; i < arr.length; i++) {
147             var temp = [];
148             temp.push(arr[i]);
149             var remain = arr.slice(0);
150             remain.splice(i,1);
151             var temp2 = fullSort(remain).concat();
152             for (var j = 0; j < temp2.length; j++) {
153                 temp2[j].unshift(temp[0]);
154                 result.push(temp2[j]);
155             }
156         }
157         return result;
158     };
159
160     //计算结果
161     function FResult() {
162             var l = [
163                  [2,3,2,1,'束带'],
164                  [1,1,2,2,'上衣'],
165                  [4,4,1,1,'护腕'],
166                  [2,4,2,1,'下摆'],
167                  [4,4,1,2,'武器'],
168                  [3,3,1,4,'玉佩'],
169                  [2,2,3,4,'项链'],
170                  [1,4,2,3,'腰带'],
171                  [1,4,4,2,'鞋子']
172             ];
173         var table = document.getElementById('table');
174         for (i=table.children.length-1;i>=1;i--){
175             table.removeChild(table.children[i]);
176         }
177         var all_l = [];
178         for (var i = 0, arr = fullSort(l); i < arr.length; i++) {
179             all_l.push(arr[i]);
180         };
181         var count_id = 0;
182         all_l.forEach(function (l1) {
183             var count=0;
184             var zuhe=[];
185             if (l1[0][0]===l1[1][2]){
186                 count+=1;
187                 zuhe.push(l1[0][0]);
188             };
189             if (l1[0][1]===l1[2][3]){
190                 count+=1;
191                 zuhe.push(l1[0][1])
192             };
193             if (l1[0][2]===l1[3][0]){
194                   count+=1;
195                   zuhe.push(l1[0][2])
196             };
197             if (l1[0][3]===l1[3][1]){
198                 count+=1;
199                 zuhe.push(l1[0][3])
200             };
201             if (l1[5][3]===l1[1][1]){
202                  count+=1;
203                  zuhe.push(l1[5][3])
204             };
205             if (l1[5][2]===l1[2][0]){
206                   count+=1;
207                   zuhe.push(l1[5][2])
208             };
209             if (l1[6][0]===l1[4][2]){
210                  count+=1;
211                  zuhe.push(l1[6][0])
212             };
213             if (l1[6][1]===l1[3][3]){
214                  count+=1;
215                  zuhe.push(l1[6][1])
216             };
217             if (l1[7][1]===l1[1][3]){
218                 count+=1;
219                 zuhe.push(l1[7][1])
220             };
221             if (l1[7][2]===l1[4][0]){
222                   count+=1;
223                   zuhe.push(l1[7][2])
224             };
225             if (l1[8][0]===l1[2][2]){
226                   count+=1;
227                   zuhe.push(l1[8][0])
228             };
229             if (l1[8][3]===l1[3][1]) {
230                 count += 1;
231                 zuhe.push(l1[8][3])
232             };
233             var lianxian = document.getElementById('lianxian');
234             if(count===(lianxian.selectedIndex+1)){
235
236
237                 //table.innerHTML='';
238                 var tr = document.createElement('tr');
239                 tr.id = 'tr'+count_id;
240
241                 for (var i=0,arr=l1;i<l1.length;i++){
242                     var td = document.createElement('td');
243                     td.innerHTML = l1[i][4];
244                     tr.appendChild(td);
245                 };
246                 var td = document.createElement('td');
247                 td.innerText=f(zuhe);
248                 function f(zuhe){
249                     var count1=0;
250                     var count2=0;
251                     var count3=0;
252                     var count4=0;
253                     zuhe.forEach(function (v) {
254                     if (v===1)count1++;
255                     if (v===2)count2++;
256                     if (v===3)count3++;
257                     if (v===4)count4++;
258                     });
259                     return '攻击(红色)'+count1+'条'+','+
260                             '速度(黄色)'+count2+'条'+','+
261                             '气血(蓝色)'+count3+'条'+','+
262                             '防御(绿色)'+count4+'条'
263                 };
264
265                 tr.appendChild(td);
266                 var td = document.createElement('td');
267                 td.innerHTML='<input type="button" value="代入" οnclick="dairu(\''+count_id+'\')">';
268                 tr.appendChild(td);
269                 table.appendChild(tr);
270                 count_id++;
271             };
272         });
273     };
274     function dairu(count_id) {
275         var dairu_tr = document.getElementById('tr'+count_id);
276         for (var i=0;i<9;i++){
277             document.getElementById('tu_td'+(i+1)).innerText = dairu_tr.children[i].innerText;
278         }
279     }
280
281 </script>
282 </body>
283 </html>

View Code

8、递归实现列表全排列

 1 #coding:utf8
 2 #1、排列[1,2,3],首先取出[1],然后排列[2,3]
 3 #2、然后取出[2],然后排列[3],递归到此返回
 4 #3、再把返回的[3]加上之前取出的数,[1]+[2]+[3]=[1,2,3]
 5 #4、从2继续,第二次取出[3],然后排列[2],递归在此返回
 6 #5、再把返回的[2]加上之前取出的数,[1]+[3]+[2]=[1,3,2]
 7 def perm(l):
 8     if len(l)==1:
 9         return [l]
10     r=[]
11     for i in range(len(l)):
12         s = l[:i] + l[i+1:]
13         p = perm(s)
14         for x in p:
15             r.append(l[i:i+1]+x)
16     return r
17
18 print(perm([1,2,3]))
19
20 #[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

View Code

9、汉诺塔

 1 #1、把n-1个盘子从A到B
 2 #2、把第n个盘子从A到C
 3 #3、把n-1个盘子从B到C
 4
 5
 6 def hanoi(n,A,B,C):
 7     if n>0:
 8         hanoi(n-1,A,C,B)
 9         print('%s->%s'%(A,C))
10         hanoi(n-1,B,A,C)
11
12 hanoi(7,"A","B","C")

View Code

转载于:https://www.cnblogs.com/cx59244405/p/8407392.html

关于递归和游戏的几个小程序相关推荐

  1. 娱乐小游戏助力神器威信小程序源码下载多种游戏选择玩法多种

    大家好今天给大家带来另外一款小神器 好像记得小编之前也发过好几款这种小程序源码吧 但是每一款的UI或者功能什么的都还是会有所不一样的 大家也可以找找之前所发的那几款对比一下然后决定自己需要哪一款哟 这 ...

  2. 适合编程初学者的开源项目:小游戏2048(微信小程序版)

    目标 为编程初学者打造入门学习项目,使用各种主流编程语言来实现. 2048游戏规则 一共16个单元格,初始时由2或者4构成. 1.手指向一个方向滑动,所有格子会向那个方向运动. 2.相同数字的两个格子 ...

  3. 小程序源码:喝酒娱乐小游戏助力神器微信小程序源码下载多种游戏选择玩法多种

    大家好今天给大家带来另外一款喝酒小神器 好像记得小编之前也发过好几款这种小程序源码吧 但是每一款的UI或者功能什么的都还是会有所不一样的 大家也可以找找之前所发的那几款对比一下然后决定自己需要哪一款哟 ...

  4. 喝酒娱乐小游戏助力神器微信小程序源码下载多种游戏选择玩法多种

    大家好今天给大家带来另外一款喝酒小神器 好像记得小编之前也发过好几款这种小程序源码吧 但是每一款的UI或者功能什么的都还是会有所不一样的 大家也可以找找之前所发的那几款对比一下然后决定自己需要哪一款哟 ...

  5. 云开发谁是卧底线下小游戏发牌助手微信小程序源码-亲测可用

    云开发谁是卧底线下小游戏源码,发牌助手微信小程序源码. "谁是卧底OL"是一个非常有趣,风靡全国的比拼语言表述能力.知识面与想象力的游戏. 谁是卧底OL是一款由开发商北游科技倾力打 ...

  6. 首份小程序广告投放价值榜单发布:游戏、工具类小程序占主导地位

    2018年3月小程序广告正式开放内测,所有类目小程序均可进行广告投放.随后众多品牌小程序纷纷开放小程序广告,近日第三方小程序开发工具即速应用和 精硕科技集团 赋能智慧商业的数据智能技术提供商nEqua ...

  7. 这款跳来跳去的小游戏,是微信小程序的年度大招

    虎嗅注:今天,应该有很多人的朋友圈都被一款跳来跳去的小游戏刷屏了.这是微信小程序年底的大招:"小游戏".其实过去小程序一直没有排斥过游戏,只是在张小龙的规划里,还"时机未 ...

  8. python谁是卧底游戏流程图_虎牙小程序—谁是卧底 |明星互动游戏

    主播连麦,观众互动每次都是唱歌跳舞聊天,你是否有为直播内容无吸引力而担心? 别担心!最经典的明星互动游戏"谁是卧底"终于来了,主播直播连麦就可以玩! 最重要的是!!!观众也可参与到 ...

  9. c语言编写数据存储的游戏,c语言经典小程序和c语言编写的小游戏带注释(自动保存的).doc...

    c语言经典小程序和c语言编写的小游戏带注释(自动保存的) 1.写一个定时?关机的小程?序,可以立即关?闭计算机,也可以一段?时间后关闭?计算机. #inclu?de #inclu?de #inclu? ...

最新文章

  1. 【 FPGA 】状态机,FPGA的灵魂
  2. C语言基础语言总结(二)
  3. 服务器开发设计之算法宝典
  4. c++中的全排列函数next_permutation()
  5. tensorflow:双线性插值反卷积
  6. 2个html文件顺序播放,CSS3两个动画顺序衔接播放
  7. 测试枕头软件,智能枕头:一款神器监测您的睡眠
  8. JAVA开发路线走向高级开发工程师
  9. 希腊字母表 ← LaTeX
  10. matlab median filter,matlab--fftshift,filter2,median用法. (转)
  11. matlab中通过滤波器,[转载]matlab 滤波器(转)
  12. 如何利用PDF转换器将WPS转换成word
  13. 【影像组学】理论学习——特征类型
  14. java作业 实现模拟保皇开始的发牌环节
  15. Docker安装JIR
  16. FXS(le88266)工作原理介绍
  17. 乘法口诀表python_如何用python编写乘法口诀表
  18. C# System.Diagnostics.Stopwatch 记录程序执行时间
  19. PX4程序编译过程解析
  20. 矩阵乘法的纯Python实现 | 离开Python库!!

热门文章

  1. 深入研究HashMap
  2. Visual Studio 2019中/MD和/MDd区别
  3. Lawliet|Python学习笔记——time库
  4. 通信工程交换传输实习报告
  5. 辅助写作软件:PPT写作助手 帮助创作多图少字文章
  6. Android四大组件之Content Provider
  7. 动态规划(pta例题)
  8. [编程题]: 过河问题
  9. Excel数据导入到hbase实战
  10. 分布式ID雪花算法-解析