# 4个一维数组的元素能组成多少个一维数组

gclove
• 1.4k

``[1,2,3,4], [5], [7,8], [9]``

``````[1,5,7,9],
[2,5,7,9],
[1,5,7,9],

/***
** 省略 ...
***/

[4,5,8,9]``````

``[1,2,3,4], [5,6], [7,8], [9]``

5 个回答
prolifes
• 11.2k
✓ 已被采纳
``````#这就是求笛卡尔积

from itertools import product
print list(product([1,2,3,4], [5, 6], [7,8], [9]))``````
zhenguoli
• 4.1k

4*2*2, 16种.

``````    \$a1 = array(1,2,3,4);
\$a2 = array(5);
\$a3 = array(7,8);
\$a4 = array(9);
foreach (\$a1 as \$k1=>\$v1){
foreach (\$a2 as \$k2=>\$v2){
foreach (\$a3 as \$k3=>\$v3){
foreach (\$a4 as \$k4=>\$v4){
\$a = array(\$v1,\$v2,\$v3,\$v4);
print_r(\$a);
echo "<br>";
}
}
}
}``````

``````var a = [1,2,3,4],
b = [5,6],
c = [7,8],
d = [9];

for(var i = 0; i < a.length; i++) {
for(var j = 0; j < b.length; j++) {
for(var k = 0; k < c.length; k++) {
for(var l = 0; l < d.length; l++) {
console.log(a[i] + '-' + b[j] + '-' + c[k] + '-' + d[l]);
}
}
}
}``````
``````function list(arr){
function newArr(arr){
var num=1;
arr.forEach((x)=>{
num*=x.length
})
var tem=[]
for(var i=0;i<num;i++){
tem[i]=[];
}
return tem
}
function numFn(num){
var pp=[];
var nums=list.length;
arr.forEach(function(x){
nums=nums/x.length
pp.push(x[nums==1?num%x.length:parseInt(num/nums)%x.length]);

})
return pp
}
var list=newArr(arr);
for(var j=0;j<list.length;j++){
list[j]=numFn(j)
}
return list
}``````

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进