zoukankan      html  css  js  c++  java
  • JavaScript 递归法排列组合二维数组2

    <html>
    	  
    	<head>
    		<title>二维数组排列组合</title>
    	</head>
    
    	<body>
    		<div id="showDiv"></div>
    	</body>
    
    	<script type="text/javascript">
    		var arrays = [ 
    			[ 
    				'1-1-雨尘', '1-2-芸芸', '1-3-简一', '1-4-乐乐'
    			]
    			, [ 
    				'2-5-小明', '2-6-花花', '2-7-数数'
    			]
    			, [ 
    				'3-8-静静', '3-9-点点', '3-10-hapday', '3-11-欢欢', '3-12-yuchen'
    			] 
    		];
    //		debugger;
    
    		/**
    		 * 递归法排列组合二维数组
    		*/
    		function doExchange(doubleArrays){
    			var len=doubleArrays.length;
    
    			if (len >= 2) {
    				var len1 = doubleArrays[0].length;
    				var len2 = doubleArrays[1].length;
    				var newlen = len1 * len2;
    				var temp = new Array(newlen);
    				var rowIndex=0;
    
    				for(var index = 0; index < len1; index++){
    					for(var cursor = 0; cursor < len2; cursor++){
    						temp[rowIndex] = doubleArrays[0][index] + '#' + doubleArrays[1][cursor];
    
    						rowIndex++;
    					}
    				}
    
    				var newArray = new Array(len-1);
    				
    				for (var index = 2; index < len; index++) {
    					newArray[index - 1] = doubleArrays[index];
    				}
    
    				newArray[0] = temp;
    
    				var result = doExchange(newArray);
    				console.log(result);
    				return result;
    			} else {
    				var result = doubleArrays[0];
    				console.log('只有一个内层数组:
    ' + result + '
    ');
    				console.log('===================================');
    				return result;
    			}
    		}
    
    	var ret = doExchange(arrays);
    
    	window.document.getElementById('showDiv').innerHTML += '共有 ' + ret.length + ' 种组合。<br /><br />';
    
    	for (var index = 0; index < ret.length; index++) {
    		var row = ret[index];
    		var rows = row.split('#');
    //		debugger;
    		for (var cursor = 0; cursor < rows.length; cursor++) {
    			var cell = rows[cursor];
    			var cells = cell.split('-');
    
    			for (var kk = 0; kk < cells.length; kk++) {
    				window.document.getElementById('showDiv').innerHTML += cells[kk] + '    '; 
    			}
    
    			window.document.getElementById('showDiv').innerHTML += '                                ';
    		}
    		window.document.getElementById('showDiv').innerHTML += '<br />';
    	}
    
    	</script>
    
    </html>
    

      

  • 相关阅读:
    So easy Webservice 2.WebService介绍
    So easy Webservice 1.Socket建设web服务
    eclipse 实用快捷键
    Memory Layout of C Programs
    C程序员必须知道的内存知识【英】
    闭包,懂不懂由你,反正我是懂了
    什么是闭包,我的理解
    浅析Java中的访问权限控制
    Java访问控制
    Java修饰符public,private,protected及默认的区别
  • 原文地址:https://www.cnblogs.com/hapday/p/10549829.html
Copyright © 2011-2022 走看看