【问题】
编号为1,2,3,4,5,6的六个人分别去坐编号为1,2,3,4,5,6的六个座位,其中有且仅有两个人的编号与座位号一致的坐法有几种?
【来源】
《高考数学排列组合题型解题研究》P8 例26
原题是小球放盒子,这里把小球换成了人,盒子换成了座位。
【数学解法】
首先,选两个人(假定为5,6)坐到相同号码的座位上,有C(6,2)种方案;
剩下的1,2,3,4四个人,要求坐到和自己编号完全不同的座位上,若有一个相同便不成立;
其次,选一个人(假定为4)做到编号为1的座位上,有C(3,1)种方案;
现在是1,2,3三个人要做到2,3,4的座位上,简单列举一下知道有
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1 共六种情况;
这六种里,第一位不能为2,第二位不能为3,因此只剩下:
1,2,3
3,1,2
3,2,1 共三种情况
综上,总的方案有C(6,2)*C(3,1)*3=15*3*3=135种
【程序解法】
程序解决此问题相对简单,思路是将6人全排列后,留下座位编号相同只有两个的就行了。代码如下:
主类代码:
package test241015;
import java.util.ArrayList;
import java.util.List;
/**
* 编号为1,2,3,4,5,6的五个人分别去坐编号为1,2,3,4,5,6的五个座位
* 其中有且仅有两个人的编号与座位号一致的坐法有几种?
*
* 2024年10月15日
*/
public class Test241012 {
public static void main(String[] args) {
int[] nums= {1,2,3,4,5,6};
Arranger arger = new Arranger(nums,nums.length);
int idx = 0;
for (List<Integer> line : arger.getResults()) {
List<Integer> corrects=new ArrayList<>();
int count=0;
for(int i=0;i<6;i++) {
if(i+1==line.get(i)) {
count++;
corrects.add(i+1);
}
}
if(count==2) {
System.out.println(String.format("%02d", ++idx) + "." + line+" @"+corrects);
}
}
}
}
全排列类代码:
package test241015;
import java.util.ArrayList;
import java.util.List;
/**
* 用于产生排列结果的工具类
* 从n个元素中取出m个元素,按照一定的顺序排成一列。得到所有排列的方案
*/
class Arranger {
// 保存在内部的对原始元素数组的引用
private int[] arr;
// 总计多少元素,此即数组长度
private final int n;
// 选多少个
private final int m;
// 返回结果
private List<List<Integer>> results;
/**
* 构造函数一
* 这个构造函数是用于全排列的(n=m=数组长度)
*
* @arr 原始元素数组
*/
public Arranger(int[] arr) {
this.arr = arr;
this.n = arr.length;
this.m = arr.length;
this.results = new ArrayList<>();
doArrange(new ArrayList<>());
}
/**
* 构造函数二
* 这个构造函数是用于部分排列的(m<n=数组长度)
*
* @param arr 原始元素数组
* @param selCnt 选多少个
*/
public Arranger(int[] arr, int selCnt) {
this.arr = arr;
this.n = arr.length;
this.m = selCnt;
if (m > n) {
throw new ArrayIndexOutOfBoundsException("m:" + m + " >n:" + n);
}
this.results = new ArrayList<>();
doArrange(new ArrayList<>());
}
/**
* 使用递归进行全排列,结果放在results中
*
* @param initialList 初始链表
*/
private void doArrange(List<Integer> initialList) {
List<Integer> innerList = new ArrayList<>(initialList);
if (m == initialList.size()) {
results.add(innerList);
}
for (int i = 0; i < arr.length; i++) {
if (innerList.contains(arr[i])) {
continue;
}
innerList.add(arr[i]);
doArrange(innerList);
innerList.remove(innerList.size() - 1);
}
}
/**
* 获得结果链表的引用
*
* @return
*/
public List<List<Integer>> getResults() {
return results;
}
// 测试
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
Arranger arranger = new Arranger(numbers);
System.out.println("四元素全排列示例:");
int idx = 0;
for (List<Integer> re : arranger.getResults()) {
System.out.println(String.format("%02d", ++idx) + "." + re);
}
/*Arranger arranger2 = new Arranger(numbers, 2);
System.out.println("\n四选二排列示例:");
idx = 0;
for (List<Integer> re : arranger2.getResults()) {
System.out.println(String.format("%02d", ++idx) + "." + re);
}*/
}
}
【程序输出结果】
01.[1, 2, 4, 3, 6, 5] @[1, 2]
02.[1, 2, 4, 5, 6, 3] @[1, 2]
03.[1, 2, 4, 6, 3, 5] @[1, 2]
04.[1, 2, 5, 3, 6, 4] @[1, 2]
05.[1, 2, 5, 6, 3, 4] @[1, 2]
06.[1, 2, 5, 6, 4, 3] @[1, 2]
07.[1, 2, 6, 3, 4, 5] @[1, 2]
08.[1, 2, 6, 5, 3, 4] @[1, 2]
09.[1, 2, 6, 5, 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, 3, 2, 5, 4] @[3, 5]
120.[6, 1, 3, 4, 2, 5] @[3, 4]
121.[6, 2, 1, 3, 5, 4] @[2, 5]
122.[6, 2, 1, 4, 3, 5] @[2, 4]
123.[6, 2, 3, 1, 4, 5] @[2, 3]
124.[6, 2, 3, 5, 1, 4] @[2, 3]
125.[6, 2, 3, 5, 4, 1] @[2, 3]
126.[6, 2, 4, 1, 5, 3] @[2, 5]
127.[6, 2, 4, 3, 5, 1] @[2, 5]
128.[6, 2, 5, 4, 1, 3] @[2, 4]
129.[6, 2, 5, 4, 3, 1] @[2, 4]
130.[6, 3, 1, 4, 5, 2] @[4, 5]
131.[6, 3, 2, 4, 5, 1] @[4, 5]
132.[6, 4, 3, 1, 5, 2] @[3, 5]
133.[6, 4, 3, 2, 5, 1] @[3, 5]
134.[6, 5, 3, 4, 1, 2] @[3, 4]
135.[6, 5, 3, 4, 2, 1] @[3, 4]
【结论】
程序输出和理论结算结果一致,可以相互印证。