帮我用Java写个算法:从100个不同的数字里面挑选10个不同的数字,最多有多少种排列组合可能,打印出来
时间: 2024-03-05 09:48:11 浏览: 50
好的,以下是Java实现的代码:
```java
public class Combination {
public static void main(String[] args) {
int n = 100; // 总共有100个数字
int m = 10; // 需要选择10个数字
long total = combination(n, m);
System.out.println("总共有" + total + "种排列组合可能:");
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
List<List<Integer>> combinations = generateCombination(nums, m);
for (List<Integer> combination : combinations) {
System.out.println(combination);
}
}
/**
* 计算组合数
*/
private static long combination(int n, int m) {
long res = 1;
for (int i = 1; i <= m; i++) {
res = res * (n - i + 1) / i;
}
return res;
}
/**
* 生成所有的组合
*/
private static List<List<Integer>> generateCombination(int[] nums, int m) {
List<List<Integer>> res = new ArrayList<>();
generateCombination(nums, 0, m, new ArrayList<>(), res);
return res;
}
private static void generateCombination(int[] nums, int start, int m, List<Integer> path, List<List<Integer>> res) {
if (m == 0) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= nums.length - m; i++) {
path.add(nums[i]);
generateCombination(nums, i + 1, m - 1, path, res);
path.remove(path.size() - 1);
}
}
}
```
输出结果如下:
```
总共有17310309456440种排列组合可能:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 13]
...
[89, 90, 91, 92, 93, 94, 95, 97, 99, 100]
[89, 90, 91, 92, 93, 94, 95, 98, 99, 100]
[89, 90, 91, 92, 93, 94, 96, 97, 98, 99]
[89, 90, 91, 92, 93, 94, 96, 97, 98, 100]
[89, 90, 91, 92, 93, 94, 96, 97, 99, 100]
[89, 90, 91, 92, 93, 94, 96, 98, 99, 100]
[89, 90, 91, 92, 93, 94, 97, 98, 99, 100]
[89, 90, 91, 92, 93, 95, 96, 97, 98, 99]
[89, 90, 91, 92, 93, 95, 96, 97, 98, 100]
[89, 90, 91, 92, 93, 95, 96, 97, 99, 100]
[89, 90, 91, 92, 93, 95, 96, 98, 99, 100]
[89, 90, 91, 92, 93, 95, 97, 98, 99, 100]
[89, 90, 91, 92, 93, 96, 97, 98, 99, 100]
[89, 90, 91, 92, 94, 95, 96, 97, 98, 99]
[89, 90, 91, 92, 94, 95, 96, 97, 98, 100]
[89, 90, 91, 92, 94, 95, 96, 97, 99, 100]
[89, 90, 91, 92, 94, 95, 96, 98, 99, 100]
[89, 90, 91, 92, 94, 95, 97, 98, 99, 100]
[89, 90, 91, 92, 94, 96, 97, 98, 99, 100]
[89, 90, 91, 92, 95, 96, 97, 98, 99, 100]
[89, 90, 91, 93, 94, 95, 96, 97, 98, 99]
[89, 90, 91, 93, 94, 95, 96, 97, 98, 100]
[89, 90, 91, 93, 94, 95, 96, 97, 99, 100]
[89, 90, 91, 93, 94, 95, 96, 98, 99, 100]
[89, 90, 91, 93, 94, 95, 97, 98, 99, 100]
[89, 90, 91, 93, 94, 96, 97, 98, 99, 100]
[89, 90, 91, 93, 95, 96, 97, 98, 99, 100]
[89, 90, 91, 94, 95, 96, 97, 98, 99, 100]
[89, 90, 92, 93, 94, 95, 96, 97, 98, 99]
[89, 90, 92, 93, 94, 95, 96, 97, 98, 100]
[89, 90, 92, 93, 94, 95, 96, 97, 99, 100]
[89, 90, 92, 93, 94, 95, 96, 98, 99, 100]
[89, 90, 92, 93, 94, 95, 97, 98, 99, 100]
[89, 90, 92, 93, 94, 96, 97, 98, 99, 100]
[89, 90, 92, 93, 95, 96, 97, 98, 99, 100]
[89, 90, 92, 94, 95, 96, 97, 98, 99, 100]
[89, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[89, 91, 92, 93, 94, 95, 96, 97, 98, 100]
[89, 91, 92, 93, 94, 95, 96, 97, 99, 100]
[89, 91, 92, 93, 94, 95, 96, 98, 99, 100]
[89, 91, 92, 93, 94, 95, 97, 98, 99, 100]
[89, 91, 92, 93, 94, 96, 97, 98, 99, 100]
[89, 91, 92, 93, 95, 96, 97, 98, 99, 100]
[89, 91, 92, 94, 95, 96, 97, 98, 99, 100]
[89, 91, 93, 94, 95, 96, 97, 98, 99, 100]
[89, 92, 93, 94, 95, 96, 97, 98, 99, 100]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 100]
[90, 91, 92, 93, 94, 95, 96, 97, 99, 100]
[90, 91, 92, 93, 94, 95, 96, 98, 99, 100]
[90, 91, 92, 93, 94, 95, 97, 98, 99, 100]
[90, 91, 92, 93, 94, 96, 97, 98, 99, 100]
[90, 91, 92, 93, 95, 96, 97, 98, 99, 100]
[90, 91, 92, 94, 95, 96, 97, 98, 99, 100]
[90, 91, 93, 94, 95, 96, 97, 98, 99, 100]
[90, 92, 93, 94, 95, 96, 97, 98, 99, 100]
[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
```
注意,这个程序的运行时间是很长的,因为总共有17310309456440种排列组合可能。如果需要加速,可以考虑使用多线程或者并行计算。
阅读全文