给定一组{1,2,3,4,5...n}的n元素,我们需要找到不相交对所有集合。
{1,2,3,4,5...n}
n
例如,如果n = 4,则输出为
{(1,2),(3,4)}, {(1,3),(2,4)}, {(1,4),(2,3)}
我什至不知道如何开始。我希望有人能给我关于使用哪种算法的建议,以及可能的一些实现细节。
编辑: 用于(n-1)递归生成的Delphi代码!从n = 2 * k个元素中设置(1 * 3 * 5 * 7 … n-1)
var A: TArray<Integer>; procedure Swap(i, j: integer); var t : integer; begin t := A[i]; A[i] := A[j]; A[j] := t; end; procedure MakePairs(Start: Integer; Pairs: string); var i: Integer; begin if Start >= Length(A) then Writeln(Pairs) else for i := Start + 1 to High(A) do begin Swap(Start + 1, i); //store used element in the array beginning MakePairs(Start + 2, Pairs + Format('(%d,%d)', [A[Start], A[Start + 1]])); Swap(Start + 1, i); //get it back end; end; begin A := TArray<Integer>.Create(1,2,3,4,5,6); //be sure that array length is even!!! MakePairs(0, ''); Writeln(PairCount);
输出:
(1,2)(3,4)(5,6) (1,2)(3,5)(4,6) (1,2)(3,6)(5,4) (1,3)(2,4)(5,6) (1,3)(2,5)(4,6) (1,3)(2,6)(5,4) (1,4)(3,2)(5,6) (1,4)(3,5)(2,6) (1,4)(3,6)(5,2) (1,5)(3,4)(2,6) (1,5)(3,2)(4,6) (1,5)(3,6)(2,4) (1,6)(3,4)(5,2) (1,6)(3,5)(4,2) (1,6)(3,2)(5,4) 15
也适用于奇数数组的 加法 变量(奇怪的顺序)
procedure MakePairs(Start: Integer; Pairs: string); var i: Integer; OddFlag: Integer; begin if Start >= Length(A) then Memo1.Lines.Add(Pairs) else begin Oddflag := (High(A) - Start) and 1; for i := Start + OddFlag to High(A) do begin Swap(Start + OddFlag, i); if OddFlag = 1 then MakePairs(Start + 2, Pairs + Format('(%d,%d)', [A[Start], A[Start + 1]])) else MakePairs(Start + 1, Pairs); Swap(Start + OddFlag, i); end; end; end;
对于(1,2,3,4,5):
(2,3)(4,5) (2,4)(3,5) (2,5)(4,3) (1,3)(4,5) (1,4)(3,5) (1,5)(4,3) (2,1)(4,5) (2,4)(1,5) (2,5)(4,1) (2,3)(1,5) (2,1)(3,5) (2,5)(1,3) (2,3)(4,1) (2,4)(3,1) (2,1)(4,3) 15