分机号

原创


 


标题:分机号

X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:

751,520,321 都满足要求,而,
766,918,201 就不符合要求。

现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?

请直接提交该数字,不要填写任何多余的内容。

 

DFS即可,即在DFS全排列(https://www.cnblogs.com/chiweiming/p/9279858.html)的算法上增加后一个数要小于前一个数的条件。

public class one {

    static long total=0L;
    static int book[]=new int[10];
    static int flag[]=new int[4];
    
    static void f(int box){
        if(box>3){
            total++;
            return;
        }
        for(int i=9;i>=0;i--){
            if(book[i]==0){    //此数字还未使用
                if(box>1){
                    if(i<flag[box-1]){
                        book[i]=1;
                        flag[box]=i;
                        f(box+1);
                        flag[box]=0;
                        book[i]=0;
                    }
                }else{
                    book[i]=1;
                    flag[box]=i;
                    f(box+1);
                    flag[box]=0;
                    book[i]=0;
                }
            }
        }
    }
    
    public static void main(String[] args) {
        f(1);
        System.out.println(total);
    }

}

答案:120

20:20:13

2018-11-29

posted @ 2018-11-29 20:20  一转身已万水千山  阅读(637)  评论(0编辑  收藏  举报