直接上代码

var test = new Array();
test[0] = new Array('a1', 'a2');
test[1] = new Array('b1', 'b2', 'b3');
test[2] = new Array('c1', 'c2');

function combin(CombinList){
	var Result = new Array();
	var CombineCount = 1;
	for(i in CombinList){
		CombineCount *= CombinList[i].length;
	}
	var RepeatTime = CombineCount;
	for(i in CombinList){
		var ClassNo = i;
		var StudentList = CombinList[i];
		RepeatTime = RepeatTime/StudentList.length;
		var StartPosition = 1;
		for(j in StudentList){
			var TempStartPosition = StartPosition;
			var SpaceCount = CombineCount/StudentList.length/RepeatTime;
			for(var J = 1; J <= SpaceCount; J ++){
				for(var I = 0; I < RepeatTime; I ++){
					if(typeof(Result[TempStartPosition + I]) == 'undefined'){
						Result[TempStartPosition + I] = new Array();
					}
					Result[TempStartPosition + I][ClassNo] = StudentList[j];
				}
				TempStartPosition += RepeatTime*StudentList.length;
			}
			StartPosition += RepeatTime;
		}
	}
	return Result;
}

document.write('<pre>');
var r = combin(test);
for(i in r){
	document.write(r[i].join('-')+' ');
	document.write('\n');
}
document.write('</pre>');

/*
得出的结果是:

a1-b1-c1 
a1-b1-c2 
a1-b2-c1 
a1-b2-c2 
a1-b3-c1 
a1-b3-c2 
a2-b1-c1 
a2-b1-c2 
a2-b2-c1 
a2-b2-c2 
a2-b3-c1 
a2-b3-c2
*/

3 Comments

  • 谢谢~~,太棒了~

    Reply
  • 算法不错,手头正好有个项目需要排列组合算法。回家自己也写写看哈

    Reply
  • The mortgage loans suppose to be essential for people, which would like to ground their own organization. As a fact, it is very comfortable to get a short term loan.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *