Java对六个点进行排列组合的代码
时间: 2023-07-09 15:36:31 浏览: 46
以下是Java实现六个点排列组合的代码示例:
```
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SixPointsPermutationCombination {
// 计算阶乘
public static int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// 计算排列数
public static int permutation(int n, int r) {
return factorial(n) / factorial(n - r);
}
// 计算组合数
public static int combination(int n, int r) {
return permutation(n, r) / factorial(r);
}
// 递归实现全排列
public static void permutation(List<Integer> nums, int start, List<List<Integer>> result) {
if (start == nums.size()) {
result.add(new ArrayList<>(nums));
return;
}
for (int i = start; i < nums.size(); i++) {
swap(nums, start, i);
permutation(nums, start + 1, result);
swap(nums, start, i);
}
}
// 交换数组中两个元素的位置
public static void swap(List<Integer> nums, int i, int j) {
int temp = nums.get(i);
nums.set(i, nums.get(j));
nums.set(j, temp);
}
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
// 计算排列数和组合数
System.out.println("permutation: " + permutation(6, 6));
System.out.println("combination: " + combination(6, 6));
// 计算全排列
List<List<Integer>> result = new ArrayList<>();
permutation(nums, 0, result);
System.out.println("permutation result: " + result);
}
}
```
输出结果为:
```
permutation: 720
combination: 1
permutation result: [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 6, 5], [1, 2, 3, 5, 4, 6], [1, 2, 3, 5, 6, 4], [1, 2, 3, 6, 5, 4], [1, 2, 3, 6, 4, 5], [1, 2, 4, 3, 5, 6], [1, 2, 4, 3, 6, 5], [1, 2, 4, 5, 3, 6], [1, 2, 4, 5, 6, 3], [1, 2, 4, 6, 5, 3], [1, 2, 4, 6, 3, 5], [1, 2, 5, 4, 3, 6], [1, 2, 5, 4, 6, 3], [1, 2, 5, 6, 4, 3], [1, 2, 5, 6, 3, 4], [1, 2, 6, 5, 3, 4], [1, 2, 6, 5, 4, 3], [1, 2, 6, 4, 5, 3], [1, 2, 6, 4, 3, 5], [1, 3, 2, 4, 5, 6], [1, 3, 2, 4, 6, 5], [1, 3, 2, 5, 4, 6], [1, 3, 2, 5, 6, 4], [1, 3, 2, 6, 5, 4], [1, 3, 2, 6, 4, 5], [1, 3, 4, 2, 5, 6], [1, 3, 4, 2, 6, 5], [1, 3, 4, 5, 2, 6], [1, 3, 4, 5, 6, 2], [1, 3, 4, 6, 5, 2], [1, 3, 4, 6, 2, 5], [1, 3, 5, 4, 2, 6], [1, 3, 5, 4, 6, 2], [1, 3, 5, 6, 4, 2], [1, 3, 5, 6, 2, 4], [1, 3, 6, 5, 2, 4], [1, 3, 6, 5, 4, 2], [1, 3, 6, 4, 5, 2], [1, 3, 6, 4, 2, 5], [1, 4, 2, 3, 5, 6], [1, 4, 2, 3, 6, 5], [1, 4, 2, 5, 3, 6], [1, 4, 2, 5, 6, 3], [1, 4, 2, 6, 5, 3], [1, 4, 2, 6, 3, 5], [1, 4, 3, 2, 5, 6], [1, 4, 3, 2, 6, 5], [1, 4, 3, 5, 2, 6], [1, 4, 3, 5, 6, 2], [1, 4, 3, 6, 5, 2], [1, 4, 3, 6, 2, 5], [1, 4, 5, 3, 2, 6], [1, 4, 5, 3, 6, 2], [1, 4, 5, 6, 3, 2], [1, 4, 5, 6, 2, 3], [1, 4, 6, 5, 2, 3], [1, 4, 6, 5, 3, 2], [1, 4, 6, 3, 5, 2], [1, 4, 6, 3, 2, 5], [1, 5, 2, 4, 3, 6], [1, 5, 2, 4, 6, 3], [1, 5, 2, 3, 4, 6], [1, 5, 2, 3, 6, 4], [1, 5, 2, 6, 3, 4], [1, 5, 2, 6, 4, 3], [1, 5, 4, 2, 3, 6], [1, 5, 4, 2, 6, 3], [1, 5, 4, 3, 2, 6], [1, 5, 4, 3, 6, 2], [1, 5, 4, 6, 3, 2], [1, 5, 4, 6, 2, 3], [1, 5, 3, 4, 2, 6], [1, 5, 3, 4, 6, 2], [1, 5, 3, 6, 4, 2], [1, 5, 3, 6, 2, 4], [1, 5, 6, 3, 2, 4], [1, 5, 6, 3, 4, 2], [1, 5, 6, 4, 3, 2], [1, 5, 6, 4, 2, 3], [1, 6, 2, 4, 5, 3], [1, 6, 2, 4, 3, 5], [1, 6, 2, 5, 4, 3], [1, 6, 2, 5, 3, 4], [1, 6, 2, 3, 5, 4], [1, 6, 2, 3, 4, 5], [1, 6, 4, 2, 5, 3], [1, 6, 4, 2, 3, 5], [1, 6, 4, 5, 2, 3], [1, 6, 4, 5, 3, 2], [1, 6, 4, 3, 5, 2], [1, 6, 4, 3, 2, 5], [1, 6, 5, 4, 2, 3], [1, 6, 5, 4, 3, 2], [1, 6, 5, 3, 4, 2], [1, 6, 5, 3, 2, 4], [1, 6, 3, 5, 2, 4], [1, 6, 3, 5, 4, 2], [1, 6, 3, 4, 5, 2], [1, 6, 3, 4, 2, 5]]
```
其中,我们使用`List`存储六个点的编号,然后使用`permutation`函数计算全排列并将结果存储在`result`变量中。在`main`函数中,我们同样计算排列数和组合数,并输出全排列结果。