【Java/算法/排列】用1,2,3,4,5,6组成没有重复数字的六位数,要求任何相邻两个数字的奇偶性不同,且1和2相邻,列举出这样的六位数。

【题干】

用1,2,3,4,5,6组成没有重复数字的六位数,要求任何相邻两个数字的奇偶性不同,且1和2相邻,这样的六位数有几个?(此题为08年浙江高考文科第17题 理科第16题)

【数学解法】

6个连续数位中,12可占用的位置是01,12,23,34,45共5个位置;

12占住后,有12和21两种排列方式;

剩下的四位形成奇偶排列,有C21*C21*C11*C11种情况;(C21指组合中的2选1,其它类同)

Sum=5*2*2*2*1*1=40种

如果做题到这里就可以了,如果要把这四十种情况都列出来请继续往下看。

【程序解法】

思路:六个数字的全排列有A66=720个,将各种情况列出来再按条件过滤掉就好了。(A66指6个数的全排列)

代码:

import java.util.ArrayList;
import java.util.List;

public class Arrange2 {
    static List<List<Integer>> results = new ArrayList<>();
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
  
        Arrange2 h1 = new Arrange2();
        h1.dfs(arr,new ArrayList<>());
        
        int idx=0;
        for (List<Integer> currLs : results) {
            
            // 1.奇偶间隔校验
            int first=currLs.get(0);
            int second=currLs.get(1);
            int third=currLs.get(2);
            int forth=currLs.get(3);
            int fifth=currLs.get(4);
            int sixth=currLs.get(5);
            
            if(first%2==0) {
                // 首个是偶数,则3,5必须是偶数,2,4,6必须是奇数
                if(third%2!=0 || fifth%2!=0) {
                    continue;
                }
                
                if(second%2!=1 || forth%2!=1 || sixth%2!=1) {
                    continue;
                }
                
            }else {
                // 首个是奇数,则3,5必须是奇数,2,4,6必须是偶数
                if(third%2!=1 || fifth%2!=1) {
                    continue;
                }
                
                if(second%2!=0 || forth%2!=0 || sixth%2!=0) {
                    continue;
                }
            }
            
            // 2.1,2捆绑校验
            if(first==1) {
                if(second!=2) {
                    continue;
                }
            }
            if(first==2) {
                if(second!=1) {
                    continue;
                }
            }
            
            if(sixth==1) {
                if(fifth!=2) {
                    continue;
                }
            }
            if(sixth==2) {
                if(fifth!=1) {
                    continue;
                }
            }
            
            boolean flag=false;
            for(int i=1;i<6;i++) {
                if(currLs.get(i)==1) {
                    if(currLs.get(i-1)!=2 && currLs.get(i+1)!=2) {
                        flag=true;
                        continue;
                    }
                }
                if(currLs.get(i)==2) {
                    if(currLs.get(i-1)!=1 && currLs.get(i+1)!=1) {
                        flag=true;
                        continue;
                    }
                }
            }
            
            if(flag) {
                continue;
            }
            
            
            System.out.println(String.format("%02d",++idx)+"."+currLs);
        }  
    }
  
    public List<List<Integer>> dfs( int[] arr,List<Integer> list){
        List<Integer> temp = new ArrayList<>(list);
        if (arr.length == list.size()){
            results.add(temp);
        }
        for (int i=0;i<arr.length;i++){
            if (temp.contains(arr[i])){
                continue;
            }
            temp.add(arr[i]);
            dfs(arr,temp);
            temp.remove(temp.size()-1);
        }
        return results;
    }
}

输出:

01.[1, 2, 3, 4, 5, 6]
02.[1, 2, 3, 6, 5, 4]
03.[1, 2, 5, 4, 3, 6]
04.[1, 2, 5, 6, 3, 4]
05.[2, 1, 4, 3, 6, 5]
06.[2, 1, 4, 5, 6, 3]
07.[2, 1, 6, 3, 4, 5]
08.[2, 1, 6, 5, 4, 3]
09.[3, 2, 1, 4, 5, 6]
10.[3, 2, 1, 6, 5, 4]
11.[3, 4, 1, 2, 5, 6]
12.[3, 4, 5, 2, 1, 6]
13.[3, 4, 5, 6, 1, 2]
14.[3, 6, 1, 2, 5, 4]
15.[3, 6, 5, 2, 1, 4]
16.[3, 6, 5, 4, 1, 2]
17.[4, 1, 2, 3, 6, 5]
18.[4, 1, 2, 5, 6, 3]
19.[4, 3, 2, 1, 6, 5]
20.[4, 3, 6, 1, 2, 5]
21.[4, 3, 6, 5, 2, 1]
22.[4, 5, 2, 1, 6, 3]
23.[4, 5, 6, 1, 2, 3]
24.[4, 5, 6, 3, 2, 1]
25.[5, 2, 1, 4, 3, 6]
26.[5, 2, 1, 6, 3, 4]
27.[5, 4, 1, 2, 3, 6]
28.[5, 4, 3, 2, 1, 6]
29.[5, 4, 3, 6, 1, 2]
30.[5, 6, 1, 2, 3, 4]
31.[5, 6, 3, 2, 1, 4]
32.[5, 6, 3, 4, 1, 2]
33.[6, 1, 2, 3, 4, 5]
34.[6, 1, 2, 5, 4, 3]
35.[6, 3, 2, 1, 4, 5]
36.[6, 3, 4, 1, 2, 5]
37.[6, 3, 4, 5, 2, 1]
38.[6, 5, 2, 1, 4, 3]
39.[6, 5, 4, 1, 2, 3]
40.[6, 5, 4, 3, 2, 1]

【参考资料】

1.《新编中学数学解题方法1000招丛书【排列和组合】》P99 刘培杰工作室编 哈尔滨工业大学出版社出版

2.https://www.jb51.net/article/252294.htm

END

 

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