计算1到n中数字1出现的次数(Java)

// 计算1-n中1出现的次数
public class CountOne {
    // 思路:分别计算“1”在每个位上面出现的次数,叠加起来
    public static int countNumOf1(int n) {
        if (n <= 0) {
            return 0;
        }
        int count = 0;
        int factor = 1;
        while(n / factor != 0) {
            int lowerNum = n - n / factor * factor;
            int currentNum = (n / factor) % 10;
            int highNum = n / (factor * 10);
            
            if (currentNum == 0) {
                // 如果为0,出现1的次数由高位决定
                count += highNum * factor;
            } else if (currentNum == 1) {
                // 如果为1,出现1的次数由高位和低位决定
                count += highNum * factor + lowerNum + 1;
            } else {
                // 如果大于1,出现1的次数由高位决定 
                count += (highNum + 1) * factor;
            }
            factor *= 10;
        }
        return count;
    }
    
    public static void main(String[] args) {
        // 测试
        System.out.println(countNumOf1(13));
        System.out.println(countNumOf1(23));
        System.out.println(countNumOf1(33));
        System.out.println(countNumOf1(93));
        System.out.println(countNumOf1(123));
 
    }
 
}

你可能感兴趣的:(计算1到n中数字1出现的次数(Java))