2

把1,2,3,4,5,6这几个数3个一组,怎么用php循环出120种可能?
如:
//123 124 125 126
//132 134 135 136
//142 143 145 146
//152,153,154,156
//162.163.164.165
//213,214,215,216
//231.234.235.236
......等等

2018-12-11 提问
2 个回答
0

已采纳
a = [1, 2, 3, 4, 5, 6]

for (let i = 0; i < a.length; ++i) {
    for (let j = 0; j < a.length; ++j) {
        if (j === i) continue;
        for (let k = 0; k < a.length; ++k) {
            if (k === i || k === j) continue;
            console.log(a[i], a[j], a[k]);
        }
    }
}
0

三重循环,简单粗暴:

int[] value = {1,2,3,4,5,6}
for(int index1=0;index1<value.length;++index1){
    for(int index2=0;index2<value.length;++index2){
        if(index2==index1) continue; // 重复的直接跳过
        for(int index3=0;index3<value.length;++index3){
            if(index3==index1 || index3 == index2) continue; 重复的跳过
            cout<<value[index1]<<value[index2]<<value[index3];
        }
    }

撰写答案

推广链接