有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?

/**
* @author User wxxu
* @description: 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
* 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
* @create 2018/5/14 20:35
*/

public class NoRepeatNum {
    public static void main(String[] args){
        int count = 0;
        //此方法时间复杂度低,还有一种是三重循环的,时间复杂度O(n3)
        for (int i = 123; i < 432; i++) {
            //百位
            int t1 = i/100;
            //十位
            int t2 = i%100/10;
            //个位
            int t3 = i%100%10;

            if(getBool(t1) && getBool(t2) && getBool(t3)){
                if(t1!=t2 && t1!=t3 && t2!=t3){
                    System.out.println(i);
                    count++;
                }
            }
        }
        System.out.println("总共:"+count);
    }

    public static boolean getBool(int t){
        boolean bool = false;
        if(t==1 || t==2 || t==3 || t==4){
            bool = true;
        }
        return bool;
    }
}

猜你喜欢

转载自blog.csdn.net/x_i_xw/article/details/80331158