【高中数学/排列组合】某校开展研学活动时进行劳动技能比赛,通过初选,选出ABCDEF共6名同学进行决赛......试分析这六人的名次排列顺序可能的结果有多少种?

【原题】

某校开展研学活动时进行劳动技能比赛,通过初选,选出ABCDEF共6名同学进行决赛,决出第一名到第六名的名次,A和B去询问成绩,回答者对A说“很遗憾,你和B都未拿到冠军”,对B说“你当然不是最差的”。试分析这六人的名次排列顺序可能的结果有多少种?

【数学解法】

1.直接法

首位只能选CDEF,有C_4_1种;

末位只能选ACDEF,且要去掉首位的一个,有C_4_1种;

中间四人进行无规则全排列,有A_4_4种;

总计有C_4_1*A_4_4*C_4_1=4*24*4=384种。

2.排除法

总体进行全排列有A_6_6种;

减去甲在首位,其余全排列的A_5_5种;

减去乙在首位,其余全排列的A_5_5种;

减去乙在末位,其余全排列的A_5_5种;

加上甲在首位乙在末位的A_4_4种,这部分是多减了的,是一三两种情况重合的部分;

总计有A_6_6-3*A_5_5+A_4_4=720-3*120+24=360+24=384种

【程序解法】

程序解法的逻辑相对简单,全排列里去掉首位有甲乙,末位有乙的情况即可,代码如下:

主类:

package test240809;

import java.util.List;

/**
 * 
 * 某校开展研学活动时进行劳动技能比赛,通过初选,选出ABCDEF共6名同学进行决赛,决出第一名到第六名的名次,A和B去询问成绩,回答者对A说“很遗憾,你和B都未拿到冠军”,对B说“你当然不是最差的”。试分析这六人的名次排列顺序可能的结果有多少种?
 *
 */
public class Test2 {
    public static void main(String[] args) {
        final String[] names= {"A","B","C","D","E","F"};
        final int[] arr1= {0,1,2,3,4,5};
        
        Arranger arranger = new Arranger(arr1,arr1.length);
        
        int idx=0;
        for (List<Integer> line : arranger.getResults()) {
            int first=line.get(0);
            int last =line.get(4);
            
            if(first==0 || first==1 || last==1) {
                continue;
            }else {
                idx++;
                
                StringBuilder sb=new StringBuilder();
                sb.append(idx+".");
                sb.append(names[line.get(0)]);
                sb.append(names[line.get(1)]);
                sb.append(names[line.get(2)]);
                sb.append(names[line.get(3)]);
                sb.append(names[line.get(4)]);
                
                System.out.println(sb.toString());
            }
        }
    }
}

辅助类Arranger:

package test240809;

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);
        }*/
    }
}

【程序输出】

1.CABDE
2.CABDF
3.CABED
4.CABEF
5.CABFD
6.CABFE
7.CADBE
8.CADBF
9.CADEF
10.CADFE
11.CAEBD
12.CAEBF
13.CAEDF
14.CAEFD
15.CAFBD
16.CAFBE
17.CAFDE
18.CAFED
19.CBADE
20.CBADF
21.CBAED
22.CBAEF
23.CBAFD
24.CBAFE
25.CBDAE
26.CBDAF
27.CBDEA
28.CBDEF
29.CBDFA
30.CBDFE
31.CBEAD
32.CBEAF
33.CBEDA
34.CBEDF
35.CBEFA
36.CBEFD
37.CBFAD
38.CBFAE
39.CBFDA
40.CBFDE
41.CBFEA
42.CBFED
43.CDABE
44.CDABF
45.CDAEF
46.CDAFE
47.CDBAE
48.CDBAF
49.CDBEA
50.CDBEF
51.CDBFA
52.CDBFE
53.CDEAF
54.CDEBA
55.CDEBF
56.CDEFA
57.CDFAE
58.CDFBA
59.CDFBE
60.CDFEA
61.CEABD
62.CEABF
63.CEADF
64.CEAFD
65.CEBAD
66.CEBAF
67.CEBDA
68.CEBDF
69.CEBFA
70.CEBFD
71.CEDAF
72.CEDBA
73.CEDBF
74.CEDFA
75.CEFAD
76.CEFBA
77.CEFBD
78.CEFDA
79.CFABD
80.CFABE
81.CFADE
82.CFAED
83.CFBAD
84.CFBAE
85.CFBDA
86.CFBDE
87.CFBEA
88.CFBED
89.CFDAE
90.CFDBA
91.CFDBE
92.CFDEA
93.CFEAD
94.CFEBA
95.CFEBD
96.CFEDA
97.DABCE
98.DABCF
99.DABEC
100.DABEF
101.DABFC
102.DABFE
103.DACBE
104.DACBF
105.DACEF
106.DACFE
107.DAEBC
108.DAEBF
109.DAECF
110.DAEFC
111.DAFBC
112.DAFBE
113.DAFCE
114.DAFEC
115.DBACE
116.DBACF
117.DBAEC
118.DBAEF
119.DBAFC
120.DBAFE
121.DBCAE
122.DBCAF
123.DBCEA
124.DBCEF
125.DBCFA
126.DBCFE
127.DBEAC
128.DBEAF
129.DBECA
130.DBECF
131.DBEFA
132.DBEFC
133.DBFAC
134.DBFAE
135.DBFCA
136.DBFCE
137.DBFEA
138.DBFEC
139.DCABE
140.DCABF
141.DCAEF
142.DCAFE
143.DCBAE
144.DCBAF
145.DCBEA
146.DCBEF
147.DCBFA
148.DCBFE
149.DCEAF
150.DCEBA
151.DCEBF
152.DCEFA
153.DCFAE
154.DCFBA
155.DCFBE
156.DCFEA
157.DEABC
158.DEABF
159.DEACF
160.DEAFC
161.DEBAC
162.DEBAF
163.DEBCA
164.DEBCF
165.DEBFA
166.DEBFC
167.DECAF
168.DECBA
169.DECBF
170.DECFA
171.DEFAC
172.DEFBA
173.DEFBC
174.DEFCA
175.DFABC
176.DFABE
177.DFACE
178.DFAEC
179.DFBAC
180.DFBAE
181.DFBCA
182.DFBCE
183.DFBEA
184.DFBEC
185.DFCAE
186.DFCBA
187.DFCBE
188.DFCEA
189.DFEAC
190.DFEBA
191.DFEBC
192.DFECA
193.EABCD
194.EABCF
195.EABDC
196.EABDF
197.EABFC
198.EABFD
199.EACBD
200.EACBF
201.EACDF
202.EACFD
203.EADBC
204.EADBF
205.EADCF
206.EADFC
207.EAFBC
208.EAFBD
209.EAFCD
210.EAFDC
211.EBACD
212.EBACF
213.EBADC
214.EBADF
215.EBAFC
216.EBAFD
217.EBCAD
218.EBCAF
219.EBCDA
220.EBCDF
221.EBCFA
222.EBCFD
223.EBDAC
224.EBDAF
225.EBDCA
226.EBDCF
227.EBDFA
228.EBDFC
229.EBFAC
230.EBFAD
231.EBFCA
232.EBFCD
233.EBFDA
234.EBFDC
235.ECABD
236.ECABF
237.ECADF
238.ECAFD
239.ECBAD
240.ECBAF
241.ECBDA
242.ECBDF
243.ECBFA
244.ECBFD
245.ECDAF
246.ECDBA
247.ECDBF
248.ECDFA
249.ECFAD
250.ECFBA
251.ECFBD
252.ECFDA
253.EDABC
254.EDABF
255.EDACF
256.EDAFC
257.EDBAC
258.EDBAF
259.EDBCA
260.EDBCF
261.EDBFA
262.EDBFC
263.EDCAF
264.EDCBA
265.EDCBF
266.EDCFA
267.EDFAC
268.EDFBA
269.EDFBC
270.EDFCA
271.EFABC
272.EFABD
273.EFACD
274.EFADC
275.EFBAC
276.EFBAD
277.EFBCA
278.EFBCD
279.EFBDA
280.EFBDC
281.EFCAD
282.EFCBA
283.EFCBD
284.EFCDA
285.EFDAC
286.EFDBA
287.EFDBC
288.EFDCA
289.FABCD
290.FABCE
291.FABDC
292.FABDE
293.FABEC
294.FABED
295.FACBD
296.FACBE
297.FACDE
298.FACED
299.FADBC
300.FADBE
301.FADCE
302.FADEC
303.FAEBC
304.FAEBD
305.FAECD
306.FAEDC
307.FBACD
308.FBACE
309.FBADC
310.FBADE
311.FBAEC
312.FBAED
313.FBCAD
314.FBCAE
315.FBCDA
316.FBCDE
317.FBCEA
318.FBCED
319.FBDAC
320.FBDAE
321.FBDCA
322.FBDCE
323.FBDEA
324.FBDEC
325.FBEAC
326.FBEAD
327.FBECA
328.FBECD
329.FBEDA
330.FBEDC
331.FCABD
332.FCABE
333.FCADE
334.FCAED
335.FCBAD
336.FCBAE
337.FCBDA
338.FCBDE
339.FCBEA
340.FCBED
341.FCDAE
342.FCDBA
343.FCDBE
344.FCDEA
345.FCEAD
346.FCEBA
347.FCEBD
348.FCEDA
349.FDABC
350.FDABE
351.FDACE
352.FDAEC
353.FDBAC
354.FDBAE
355.FDBCA
356.FDBCE
357.FDBEA
358.FDBEC
359.FDCAE
360.FDCBA
361.FDCBE
362.FDCEA
363.FDEAC
364.FDEBA
365.FDEBC
366.FDECA
367.FEABC
368.FEABD
369.FEACD
370.FEADC
371.FEBAC
372.FEBAD
373.FEBCA
374.FEBCD
375.FEBDA
376.FEBDC
377.FECAD
378.FECBA
379.FECBD
380.FECDA
381.FEDAC
382.FEDBA
383.FEDBC
384.FEDCA

【结论】

三种方法结果可以相互印证,程序为数学方法的有益补充。

END

 

posted @ 2013-08-21 15:35  逆火狂飙  阅读(9)  评论(1编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东