Java basic arithmetic questions (11): 1,2,3,4 figures, the number of other with no repeat of the three-digit numbers can be composed? How much are?

View all 50 basic arithmetic questions, see:

Java basic algorithm of 50 questions

package Demo11Number_Combine;
public class Number_Combine {
    /**
     * 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
     */
    /*
    分析:有四个数,组成三位数,三位数就是a*100+b*10+c这样
          三重循环来完成这个题目,
          需要注意的一点是题目要求组成的数字中不能有重复数字。
     */
    public static void main(String[] args) {
        // 定义一个变量来计数,为了我们格式化打印,每行打印10个数,初始化为1
        int count =1;
        // 使用三重循环,来遍历百位,十位及个位的数值
        for (int i = 1; i <= 4; i++) {
            for (int k = 1; k <= 4; k++){
                for (int j = 1; j <= 4; j++) {
                    // 这三重循环会遍历所有的组成结果,包括122这种类型的
                    // 根据题目要求,我们要过滤掉有重复数字的组合结果
                    if(!(i==j || i==k || j==k)){
                        // 添加一个判断,格式化打印,每10个开始换行
                        if(count%10==0) {
                            System.out.println(i * 100 + j * 10 + k);
                            count++;
                        }else{
                            System.out.print(i*100+j*10+k+"---");
                            count++;
                        }
                    }
                }
            }
        }
        System.out.println();
        // 打印总数,由于count初始化为1,所以这里要减去1
        System.out.println("总共能组成"+(count-1)+"个数字。");
    }
}
Published 22 original articles · won praise 1 · views 1418

Guess you like

Origin blog.csdn.net/weixin_44803446/article/details/105354821