代码思考题玩玩
题目一:
思路参考:参考
传入一个从0开始顺序数组,每个数差值不同。从第一个元素依次往后跳,
每次跳跃只能跳前与前一次跳跃差值k的k-1、k或k+1个单位。输出传入的数组能否成功跳到终点。
示例 1:
[0,1,3,5,6,8,12,17]
返回 true。
即可以成功通过,按照如下方案跳跃:
跳1个单位到第2项1, 然后跳2个单位到第3项3, 接着
跳2个单位到第4项5, 然后跳3个单位到第6项8,
跳4个单位到第7项12, 最后,跳5个单位到第8项17。
示例 2:
[0,1,2,3,4,8,9,11]
返回 false。
没有办法到终点。
这是因为第5和第6项之间的间距太大,从2跳到4后,只能再最大跳3格,到不了8。
自己分析:
1. 升序(非负数)
2. 第一位永远是0
3. 初始第一步假设为1
# Py (有待修订)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "loki" """ [0, 1, 3, 5, 6, 8, 12, 17] [0, 1, 2, 3, 4, 8, 9, 11] 数组跳跃 """ userInput = [0, 1, 3, 5, 6, 8, 12, 17] userInput2 = [0, 1, 2, 3, 4, 8, 9, 11] userInput3 = [3, 2, 1, 0, 4] def number_jump(user_list): # max_pos 为最列表远长度初始值 # length 为列表长度 length, max_pos = len(user_list), 0 for i in range(length): if max_pos == 0: max_pos += 1 if i <= max_pos: max_pos = max(i + user_list[i], max_pos) # print(i, user_list[i], max_pos) if max_pos >= length - 1: return True return False def main(): print("情况一", number_jump(userInput)) print("情况二", number_jump(userInput2)) print("情况三", number_jump(userInput3)) if __name__ == '__main__': main()
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var canCross = function (stones) { 2 // 确定状态 3 let ability = new Array(stones.length) // 存每一个单元格是否有解 4 let step = {} // 存跳到每一个单元格的步跳跃距离可能性 5 6 // 初始化数据 7 ability[0] = true 8 step[stones[0]] = new Set().add(0) 9 10 // 第二个单位格开始遍历 11 for (let i = 1; i < ability.length; i++) { 12 13 ability[i] = false 14 step[stones[i]] = new Set() 15 for (let j = 0; j < i; j++) { 16 for (let k of step[stones[j]]) { 17 if (ability[j] && (stones[i] - stones[j] >= k - 1 && stones[i] - stones[j] <= k + 1)) { 18 ability[i] = true 19 // 存储所有可以到达该单元格的步数 20 step[stones[i]].add(stones[i] - stones[j]) 21 } 22 } 23 } 24 } 25 // 返回最后一个点是否到达情况 26 return ability[ability.length - 1] 27 }; 28 document.write('[0,1,3,5,6,8,12,17]'+':'+canCross([0,1,3,5,6,8,12,17])+'</br>') 29 document.write('[0,1,2,3,4,8,9,11]'+':'+canCross([0,1,2,3,4,8,9,11])+'</br>')
题目二:
写一个JS函数,传入一个一维数组表示1只股票每日收盘股价,输出可获得最大收益的一次买入与卖出时间点。
例:
输入: [4,5,3,5,6,8,6,3,2,5]
输出:第3天买入 第6天卖出 收益5
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 let list = [7, 7, 8, 3, 6, 2, 3, 3]; 2 let list2 = [4, 5, 3, 5, 6, 8, 6, 3, 2, 5]; 3 function getMaxValue(arr) { 4 let maxEarnings = 0; //最大收益 5 let min, max; 6 for (let i = 0; i < arr.length; i++) { 7 for (let j = i + 1; j < arr.length; j++) { 8 if (arr[j] - arr[i] > maxEarnings) { 9 maxEarnings = arr[j] - arr[i]; 10 min = i; 11 max = j; 12 } 13 } 14 } 15 return `第${min + 1}天买入,第${max + 16 1}天卖出,最大收益是${maxEarnings}`; 17 } 18 console.log(getMaxValue(list)); 19 console.log(getMaxValue(list2));
# Js(带样式)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title></title> 8 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js" type="text/javascript" charset="utf-8"></script> 9 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 10 </head> 11 <body> 12 <div id="app"> 13 <input type="text" v-model="gp" style="width: 550px;"> 14 <span style="color: red;">输入股票价格','分割</span> 15 <div v-for="sy in shouyi"> 16 <span>第{{sy.s}}天-第{{sy.e}}天,收益{{sy.y}}</span> 17 </div> 18 </div> 19 <script type="text/javascript"> 20 let app = new Vue({ 21 el: "#app", 22 data: { 23 gp: '6,4,5,6,7,12,56,24', 24 shouyi:[] 25 }, 26 watch: { 27 gp(newValue, oldValue) { 28 this.shouyi = [] 29 let arr = this.gp.split(',') 30 // console.log(arr) 31 this.findMax(arr) 32 } 33 }, 34 methods: { 35 findMax(arr) { 36 let $this = this 37 _.forEach(arr,function(value, key){ 38 if(key>0){ 39 let prev_arr = arr.slice(0,key) 40 let prev_arr_min = _.minBy(prev_arr,function(o){return parseInt(o)}) 41 //最小值index 42 if(value-prev_arr_min>0){ 43 let min_arr = $this.findIndx(prev_arr,prev_arr_min) 44 $this.shouyi.push({s:min_arr,e:key+1,y:value-prev_arr_min}) 45 $this.shouyi = _.sortBy($this.shouyi, ['y']).reverse(); 46 console.log('第'+min_arr+'天买入,第'+(key+1)+'天卖出,收益'+(value-prev_arr_min)) 47 } 48 } 49 }) 50 }, 51 findIndx(arr,num){ 52 let index_arr = [] 53 console.log(arr) 54 console.log(num) 55 _.forEach(arr,function(value, key){ 56 console.log(key) 57 if(parseInt(value)==num){ 58 console.log(key) 59 index_arr.push(key+1) 60 } 61 }) 62 return index_arr 63 } 64 }, 65 66 }) 67 </script> 68 </body> 69 </html>
题目三:
传入一个带“(”和“)”括号的字符串,删除最少的括号,使得输入有效,返回所有可能的结果。
例:
输入: "()())()"
输出: ["()()()", "(())()"]
输入: "(a)())()"
输出: ["(a)()()", "(a())()"]
# Py 笨方法(带修订,仅能去掉多余括号,不能得出更多结果)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "loki" def regroup(data): res = [] left_num = 0 right_num = 0 for i in data: if i == "(": left_num += 1 else: right_num += 1 res.append(i) while 1: if left_num > right_num: res.pop(res.index("(")) left_num -= 1 continue elif left_num < right_num: res.pop(res.index(")")) right_num -= 1 continue else: break return "".join(res) def main(): user_input = "()())()" print(user_input) print("fix--> ", regroup(user_input)) if __name__ == '__main__': main()
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var set=[]; 2 3 var input; 4 5 6 //去多余的括号 7 function test5(s){ 8 input = s; 9 var left = 0, right = 0; 10 for (var i = 0; i < s.length; i++) { 11 var c = s.charAt(i); 12 if (c == '(') { 13 left++; 14 } else if (c == ')') { 15 if (left == 0) { 16 right++; 17 } else { 18 left--; 19 } 20 } 21 } 22 removeInvalidParentheses(0, "", 0, 0, left, right); 23 24 var res=unique1(set); 25 26 document.write("输入值:\""+s+"\" ; "); 27 document.write("输出值:["); 28 for(var j = 0;j < res.length; j++){ 29 if(j==res.length-1){ 30 document.write("\""+res[j]+"\""); 31 }else{ 32 document.write("\""+res[j]+"\","); 33 } 34 35 } 36 document.write("]<br/>"); 37 38 39 } 40 41 //去重 42 function unique1(arr){ 43 var hash=[]; 44 for (var i = 0; i < arr.length; i++) { 45 if(hash.indexOf(arr[i])==-1){ 46 hash.push(arr[i]); 47 } 48 } 49 return hash; 50 } 51 52 function removeInvalidParentheses(index,valid,leftCount,rightCount,leftRem,rightRem){ 53 if (index == input.length) { 54 if (leftRem == 0 && rightRem == 0) { 55 set.push(valid); 56 } 57 return; 58 } 59 var c = input.charAt(index); 60 if (c == '(') { 61 if (leftRem > 0) { 62 removeInvalidParentheses(index + 1, valid, leftCount, rightCount, leftRem - 1, rightRem); 63 } 64 removeInvalidParentheses(index + 1, valid + c, leftCount + 1, rightCount, leftRem, rightRem); 65 } else if (c == ')') { 66 if (rightRem > 0) { 67 removeInvalidParentheses(index + 1, valid, leftCount, rightCount, leftRem, rightRem - 1); 68 } 69 if (rightCount < leftCount) { 70 removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount + 1, leftRem, rightRem); 71 } 72 } else { 73 removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount, leftRem, rightRem); 74 } 75 76 } 77 78 test5("(a)()))(()");
题目四:
城市的天际线是从远处观看该城市所有建筑物形成的外部轮廓,输入所有建筑物的X,Y,Height坐标,返回天际线关键点。
例:
输入:buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
输出:[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
提示:
1、 每个关键点较上个关键点的X,Height均发生变化。
2、 城市天际线问题 网上有原题
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<script> function test6(arr){ var res; document.write("输入值:"+print_arr2(arr)+" ; "); if(arr.length==0){ document.write("输出值:[] ; <br/>"); }else{ res=getSkyline(arr); document.write("输出值:"+print_arr2(res)+" ; <br/>"); } } //输出二位数组 function print_arr2(res){ var str="["; for(var i = 0;i < res.length; i++){ str=str+"["; for(var j = 0;j < res[i].length; j++){ if(j==res[i].length-1){ str=str+res[i][j]; }else{ str=str+res[i][j]+","; } } if(i==res.length-1){ str=str+"]"; }else{ str=str+"],"; } } str=str+"]"; return str; } function getSkyline(buildings){ var height = []; var res = []; var line= []; var pre = 0; var cur = 0; //将矩阵的左右边界以及其高度放入数组中,为了区分左右边界,将高度分为正负值 //即可 for(var i=0;i<buildings.length;i++){ var tmp=[]; var tmp1=[]; tmp[0] = buildings[i][0]; tmp[1] = -buildings[i][2]; height.push(tmp); tmp1[0] = buildings[i][1]; tmp1[1] = buildings[i][2]; height.push(tmp1); } //将数组按照横坐标进行排序 height.sort(function(a,b){return (a[0]-b[0]);}); //将0加入multiset,为了能够在遍历的过程中可以得到两个不重叠矩阵的高度为0的 //坐标位置 line.push(0); //遍历所有的矩阵的边界状态 for(var j=0; j<height.length; j++){ if(height[j][1]<0){ line.push(-height[j][1]); }else{ line.splice(line.indexOf(height[j][1]),1); } line.sort(function(a,b){return a-b;}); cur = line[line.length-1]; if(cur!=pre){ if(j>0&&res[res.length-1][0]==height[j][0]){ if(cur>pre){ res[res.length-1][1]=cur; } }else{ res.push([height[j][0], cur]); } pre = cur; } } return res; } test6([[2,9,10],[2,7,15],[5,12,12],[5,12,17],[15,20,10],[19,24,8]]); test6([[2,9,10]]); test6([[2,9,10],[3,7,15]]); test6([[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]); test6([[2,9,10],[2,7,15],[5,12,12],[15,20,10],[19,24,8]]); test6([[2,9,18],[2,7,15],[5,12,12],[15,20,10],[19,24,8]]); </script>
题目五:编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。
示例 1:
输入:IP = "172.16.254.1"
输出:"IPv4"
示例 2:
输入:IP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
输出:"IPv6"
示例 3:
输入:IP = "256.256.256.256"
输出:"Neither"
示例 4:
输入:IP = "2001:0db8:85a3:0:0:8A2E:0370:7334:"
输出:"Neither"
示例 5:
输入:IP = "x1.4.5.6"
输出:"Neither"
# Js(带界面)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title></title> 8 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js" type="text/javascript" charset="utf-8"></script> 9 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> 10 </head> 11 <body> 12 <div id="app"> 13 <span>ipv4:192.168.1.1</span><br> 14 <span>ipv6:2001:0db8:85a3:0:0:8A2E:0370:7334</span><br> 15 <input type="text" v-model="ip" style="width: 550px;"> 16 <span style="color: red;" v-if="!flag">输入正确的ip地址</span> 17 <span style="color: green;" v-if="flag">正确的ip地址</span> 18 </div> 19 <script type="text/javascript"> 20 let app = new Vue({ 21 el: "#app", 22 data: { 23 ip: '', 24 flag: false 25 }, 26 watch: { 27 ip(newValue, oldValue) { 28 ip = this.ip 29 console.log(this.isIPv4(ip)) 30 console.log(this.isIPv6(ip)) 31 if(this.isIPv4(ip) || this.isIPv6(ip)) 32 { 33 this.flag = true 34 }else{ 35 this.flag = false 36 } 37 } 38 }, 39 methods: { 40 isIPv6(str){ 41 if(str.indexOf(":") >= 0){//需要加上判断:号是否存在,因为如果不存在冒号,就没必要走进来判断;同时如果没有冒号,会报错:cann't read "length" of null 42 return str.match(/:/g).length<=7 && /::/.test(str) ? /^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i.test(str) : /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i.test(str); 43 } 44 }, 45 isIPv4(str){ 46 var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式 47 if(re.test(str)){ 48 if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) 49 return true; 50 }; 51 } 52 }, 53 54 }) 55 </script> 56 </body> 57 </html>
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function test(){ 2 var array1 = [1,2,3]; 3 var array2 = [2,7,8]; 4 var arr =[array1,array2]; 5 var obj = {}; 6 arr = arr 7 .reduce((a, b) => a.concat(b), []) 8 .sort((a, b) => b - a); 9 for (var i=0;i<arr.length;i++) { 10 document.write(arr[i]+" "); 11 } 12 13 } 14 function test2(str){ 15 document.write("输入:"+str+" <br/>"); 16 //截取纯数字字符串 17 var str_arr=str.match(/\d+/g); 18 //替换每部分得字符串 19 var str1 = str; 20 for(var i=0;i<str_arr.length;i++){ 21 if(i==0){ 22 var str1 =str1.replace(str_arr[i],parseInt(str_arr[i].split('').reverse().join('')).toString()); 23 }else{ 24 var str1 = str1.replace(str_arr[i],str_arr[i].split('').reverse().join('')); 25 } 26 27 } 28 document.write("输出:"+str1+"<br/>"); 29 } 30 function test3(ip){ 31 if(isIPv6(ip)){ 32 document.write("IPv6<br/>"); 33 }else if(isIPv4(ip)){ 34 document.write("IPv4<br/>"); 35 }else{ 36 document.write("Neither<br/>"); 37 } 38 } 39 40 function isIPv6(tmpstr) 41 { 42 //CDCD:910A:2222:5498:8475:1111:3900:2020 43 var patrn=/^([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$/i; 44 var r=patrn.exec(tmpstr) 45 if(r) 46 { 47 return true; 48 } 49 if(tmpstr=="::"){ 50 return true; 51 } 52 //F:F:F::1:1 F:F:F:F:F::1 F::F:F:F:F:1格式 53 patrn=/^(([0-9a-f]{1,4}:){0,6})((:[0-9a-f]{1,4}){0,6})$/i; 54 r=patrn.exec(tmpstr); 55 if(r) 56 { 57 var c=cLength(tmpstr); 58 if(c<=7 && c>0) 59 { 60 return true; 61 } 62 } 63 //F:F:10F:: 64 patrn=/^([0-9a-f]{1,4}:){1,7}:$/i; 65 r=patrn.exec(tmpstr); 66 if(r) 67 { 68 return true; 69 } 70 //::F:F:10F 71 patrn=/^:(:[0-9a-f]{1,4}){1,7}$/i; 72 r=patrn.exec(tmpstr); 73 if(r) 74 { 75 return true; 76 } 77 //F:0:0:0:0:0:10.0.0.1格式 78 patrn=/^([0-9a-f]{1,4}:){6}(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i; 79 r=patrn.exec(tmpstr); 80 if(r) 81 { 82 if(r[2]<=255 && r[3]<=255 &&r[4]<=255 && r[5]<=255 ) 83 return true; 84 } 85 //F::10.0.0.1格式 86 patrn=/^([0-9a-f]{1,4}:){1,5}:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i; 87 r=patrn.exec(tmpstr); 88 if(r) 89 { 90 if(r[2]<=255 && r[3]<=255 &&r[4]<=255 && r[5]<=255 ) 91 return true; 92 } 93 //::10.0.0.1格式 94 patrn=/^::(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i; 95 r=patrn.exec(tmpstr); 96 if(r) 97 { 98 if(r[1]<=255 && r[2]<=255 &&r[3]<=255 && r[4]<=255) 99 return true; 100 } 101 return false; 102 } 103 104 function isIPv4(str) 105 { 106 var match = str.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); 107 return match != null && 108 match[1] <= 255 && match[2] <= 255 && 109 match[3] <= 255 && match[4] <= 255; 110 } 111 112 113 test3("172.16.254.1"); 114 test3("2001:0db8:85a3:0:0:8A2E:0370:7334"); 115 test3("256.256.256.256"); 116 test3("2001:0db8:85a3:0:0:8A2E:0370:7334:"); 117 test3("x1.4.5.6");
# Js(简洁版)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <script> 2 var validIPAddress = function(IP) { 3 var ipv4 = /^((\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.){4}$/; 4 var ipv6 = /^(([\da-fA-F]{1,4}):){8}$/ 5 return ipv4.test(IP + ".") ? "IPv4" : ipv6.test(IP + ":") ? "IPv6" : "Neither"; 6 } 7 8 document.write(validIPAddress('192.168.1.255')) 9 </script>
题目六:写一个JS函数,传入任意有符号数字,输出每位数字翻转结果,符号不翻转,小数点前后分别翻转。
例:
输入:-361.210 输出:-163.012
输入:120 输出:21
#Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title></title> 8 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js" type="text/javascript" charset="utf-8"></script> 9 </head> 10 <body> 11 <script type="text/javascript"> 12 let num_1 = '-361.210' 13 let num_2 = '120' 14 function revers_num(num_1){ 15 num_1 = num_1.toString() 16 let arr = num_1.split('.') 17 let str = '' 18 let flag = 1 19 let arr_sp = arr[0].split('') 20 let arr_sp_h = _.head(arr_sp) 21 // console.log(arr_sp_h) 22 if(arr_sp_h=='-'){ 23 arr_sp = _.drop(arr_sp) 24 // console.log(arr_sp) 25 flag = 0 26 } 27 arr[0] = _.join(_.concat(_.reverse(arr_sp)),'') 28 // console.log(arr[0]) 29 str = flag?arr[0]:'-'+arr[0] 30 if(arr.length>1){ 31 arr[1] = _.join(_.concat(_.reverse(arr[1].split(''))),'') 32 str += '.'+arr[1] 33 } 34 console.log(parseFloat(str)) 35 document.write(parseFloat(str)) 36 document.write('<br>') 37 } 38 revers_num(num_1); 39 revers_num(num_2); 40 </script> 41 </body> 42 </html>
题目七:写一个JS函数,传入任意两个纯数字内容1维数组,合并成一个数组并按元素值大小排倒叙后输出该数组。
例:
输入:array1 = [1,2,3], array2 = [2,7,8]
输出:8,7,3,2,2,1
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function so(arr1,arr2){ 2 let arr=arr1.concat(arr2); 3 arr.sort(function(a,b){ 4 return b-a 5 }) 6 return arr 7 } 8 console.log(so([1,2,3],[2,7,8]))
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var mycars1 = [1,2,3]; 2 var mycars2 = [2,7,8]; 3 mycars2.forEach((res) => { 4 mycars1.push(res); 5 }); 6 var x=mycars1.sort(function(a, b) { 7 return b - a 8 }); 9 document.write(x);
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function test(){ 2 3 var array1 = [1,2,3]; 4 var array2 = [2,7,8]; 5 var arr =[array1,array2]; 6 var obj = {}; 7 8 arr = arr 9 .reduce((a, b) => a.concat(b), []) 10 .sort((a, b) => b - a); // 如果需要从小到大排序加上这个 11 12 console.log(arr); 13 } 14 test();
题目八:
热身题目:写一个JS函数,生成99乘法表。
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
.....
# Py for 循环版本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
for i in range(1, 10): for j in range(1, i+1): print(j, "*", i, "=", i*j, end=" ") print("")
# Js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function test(){ 2 for(var i=1;i<=9;i++){ 3 document.write("<br>") 4 for(var j=1;j<=i;j++){ 5 sum = i * j; 6 document.write(j ,"*",i ,"=",sum," "); 7 } 8 9 } 10 11 } 12 test();
# Py while 循环版本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
n = 1 res = 1 while n <= 9: i = 1 while i <= n: res = i * n print('%d*%d=%d' % (i, n, res), end=' ') i = i+1 print('') n = n + 1
# Js 带表格边框
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!doctype html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Document</title> 7 <!-- CSS样式 --> 8 <style type="text/css"> 9 td { 10 border: blue 1px solid; 11 } 12 </style> 13 <!-- JS实现 --> 14 <script type="text/javascript"> 15 document.write("99乘法表"); 16 document.write("<br />"); 17 document.write("<table algin='center'>"); 18 for (var i = 1; i <= 9; i++) { 19 document.write("<tr>"); 20 for (var j = 1; j <= i; j++) { 21 document.write("<td>" + j + "*" + i + "=" + (j * i) + "</td>"); 22 } 23 document.write("</tr>"); 24 } 25 document.write("</table>"); 26 </script> 27 </head> 28 29 <body> 30 </body> 31 32 </html>