# 排列组合中的分堆可能性问题

dubbo
• 30

function all_groups(arr){
}

[

``````[[a],[b],[c]],
[[a,b],[c]],
[[a],[b,c]],
[[a,c],[b]],
[[abc]]``````

]

function group_divide(k,n){
}

``````<script>
//列出所有组合方法
//输入：combination([1,2,3], 2)
//输出：[[1,2],[1,3],[2,3]]
function combination (arr, size) {
var r = []; //result

function _(t, a, n) { //tempArr, arr, num
if (n === 0) {
r[r.length] = t;
return;
}
for (var i = 0, l = a.length - n; i <= l; i++) {
var b = t.slice();
b.push(a[i]);
_(b, a.slice(i + 1), n - 1);
}
}
_([], arr, size);
return r;
}
console.debug(JSON.stringify(combination([1,2,3], 2)));

//列出所有排列方法
//arrangement([1,2,3], 2)
//输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
function arrangement(input) {
var permArr = [],
usedChars = [];
function main(input){
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
main(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
}
return main(input);
}
console.debug(JSON.stringify(arrangement([1,2,3])));

//去重
/*输入 var arr = [
[[1,2],[3,4]],
[[1,3],[2,4]],
[[1,4],[3,2]],
[[3,4],[1,2]],
[[3,1],[2,4]]
];
*/
//输出：[[["1","2"],["3","4"]],[["1","3"],["2","4"]],[["1","4"],["2","3"]]]
function remove_duplicate(arr){
var newArr = [];
var stringArr = [];
for (i = 0; i < arr.length; i++) {
var inString = [];
for (j = 0; j < arr[i].length; j++) {
inString[j] = arr[i][j].sort().join("_");
}
var outString = inString.sort().join("|");
if(stringArr.indexOf(outString) === -1) {
stringArr.push(outString);
}
}
for (i = 0; i < stringArr.length; i++) {
var outArr = stringArr[i].split("|");
newArr[i] = [];
for (j = 0; j < outArr.length; j++) {
var inArr = outArr[j].split("_");
newArr[i][j] = inArr;
}
}
return newArr;
}

var arr = [
[[1,2],[3,4]],
[[1,3],[2,4]],
[[1,4],[3,2]],
[[3,4],[1,2]],
[[3,1],[2,4]]
];
console.debug(JSON.stringify(remove_duplicate(arr)));

//var a1 = ['a', 'b'];
//var a2 = ['a', 'b', 'c', 'd'];
//return ["c", "d"]
function arr_diff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
};

//补充数组
//输入combination_left ([1,2], [1,2,3])
//输出[[1,2],[3]]
function combination_left (arr, oArr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
var diff = arr_diff(oArr, arr[i]);
newArr[i] = [];
newArr[i][0] = arr[i];
newArr[i][1] = diff;
}
return newArr;
}

</script>``````

2 个回答
✓ 已被采纳

• {1,2}, {3}

• {1,2}, {3}, {4}

• {1,2,4}, {3}

• {1,2}, {3,4}

https://paiza.io/projects/new

1,Scala

``````object Main extends App{
def ps[A]: Seq[A] => Iterator[Seq[Seq[A]]] = {
case Seq() => Iterator(Nil)
case x +: rs => (ps(rs) map (ys => List(x)+:ys)) ++
(ps(rs) flatMap { ys => ys.indices map (i => ys.updated(i, x+:ys(i))) })
}

ps("abcd") foreach println
}``````

2,JAVA

``````import java.io.BufferedReader;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
int n = 4;
ArrayList<ArrayList<ArrayList<Integer>>> array = Partition(n);
PrintResult(array);
}

private static ArrayList<ArrayList<Integer>> Clone(ArrayList<ArrayList<Integer>> element) {

ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < element.size(); i++) {
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> ca = (ArrayList<Integer>) element.get(i);
for (int j = 0; j < ca.size(); j++)
}
return result;
}

private static ArrayList<ArrayList<ArrayList<Integer>>> Partition(int n) {
ArrayList<ArrayList<ArrayList<Integer>>> array = new ArrayList<ArrayList<ArrayList<Integer>>>();
if (n == 1) {
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> b = new ArrayList<Integer>();
return array;
}
ArrayList<ArrayList<ArrayList<Integer>>> tmpArray = Partition(n - 1);
for (int i = 0; i < tmpArray.size(); i++) {
ArrayList<ArrayList<Integer>> element = (ArrayList<ArrayList<Integer>>) tmpArray.get(i);

ArrayList<ArrayList<Integer>> newelement = null;
for (int j = 0; j < element.size(); j++) {
newelement = Clone(element);

}
newelement = Clone(element);
newelement = (ArrayList<ArrayList<Integer>>) element.clone();
ArrayList<Integer> app = new ArrayList<Integer>();
}
return array;
}

private static void PrintResult(ArrayList<ArrayList<ArrayList<Integer>>> array) {
for (int i = 0; i < array.size(); i++) {
ArrayList<ArrayList<Integer>> a = (ArrayList<ArrayList<Integer>>) array.get(i);
for (int j = 0; j < a.size(); j++) {
ArrayList<Integer> b = (ArrayList<Integer>) (a.get(j));
for (int k = 0; k < b.size(); k++)
System.out.print(b.get(k));
System.out.print(" ");
}
System.out.println();
}
}
}
``````

``````    function Stirling(n, k, f) {
if (n == 0 && k == 0) { f([]); return; }
if (n == 0 || k == 0) { return; }
Stirling(n-1, k, function(s) {
for (var i = 0; i < k; i++) {
s[i].push(n);
f(s);
s[i].pop();
}
});
Stirling(n-1, k-1, function(s) {
s.push([n]);
f(s);
s.pop();
});
}
function logToConsole(s) {
console.log(JSON.stringify(s));
}
function Bell(n, f) {
for (var i = 1; i <= n; i++) {
Stirling(n, i, f);
}
}

Bell(3, logToConsole);``````

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