用java编程在10000到99999中的数字中找到aabcc类型的数字

package com.diyo.offer;

public class FindNum {
    public static void main(String[] args) {
        int count = 0;// 用于统计找的AABCC类型的数字的个数
        for (int i = 10000; i <= 99999; i++) {
            if (isNum(i)) {
                System.out.print(i + "\t");
                count++;
                if (count % 5 == 0) {// 每找到5个数字,换一行打印
                    System.out.println();
                }
            }
        }
        System.out.println("\n10000-99999范围内的aabcc类型的数字共有:" + count + "个");
    }

    private static boolean isNum(int x) {
        int a = x / 10000;// 万位
        int b = x % 10000 / 1000;// 千位
        int c = x % 1000 / 100;// 百位
        int d = x % 100 / 10;// 十位
        int e = x % 10;// 个位
        // a=b 且 d=e 且 a!=c c!=d且 a!=d
        return a == b && d == e && a != c && c != d && a != d;
    }
}

猜你喜欢

转载自www.cnblogs.com/Diyo/p/11429018.html