关于递归和游戏的几个小程序
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]))
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)
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')
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))
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)
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="计算" onclick="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.onload=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="代入" onclick="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>
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]]
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")