• JAVA经典百题之9的次数


    题目:统计1~100之间出现了几次数字9

    程序分析:
    要统计1到100之间出现了几次数字9,可以采用三种不同的方法来实现。以下是三种不同方法的解题思路、代码示例以及各自的优缺点:

    方法1: 遍历法
    思路:

    • 使用循环遍历1到100之间的每个数字,然后对每个数字进行取余和除法操作,以检查其中是否包含数字9。
    public class CountNinesMethod1 {
        public static void main(String[] args) {
            int count = 0;
            for (int i = 1; i <= 100; i++) {
                int num = i;
                while (num > 0) {
                    if (num % 10 == 9) {
                        count++;
                    }
                    num /= 10;
                }
            }
            System.out.println("数字9出现的次数: " + count);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    优点:

    • 直观易懂。
    • 适用于小范围的数字。

    缺点:

    • 效率较低,需要遍历1到100的每个数字。

    方法2: 字符串法
    思路:

    • 将1到100的每个数字转换为字符串,然后使用字符串的charAt方法来检查每个字符是否是数字9。
    public class CountNinesMethod2 {
        public static void main(String[] args) {
            int count = 0;
            for (int i = 1; i <= 100; i++) {
                String numStr = String.valueOf(i);
                for (int j = 0; j < numStr.length(); j++) {
                    if (numStr.charAt(j) == '9') {
                        count++;
                    }
                }
            }
            System.out.println("数字9出现的次数: " + count);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    优点:

    • 简单易实现。
    • 适用于小范围的数字。

    缺点:

    • 效率较低,需要频繁进行字符串转换操作。

    方法3: 数学法
    思路:

    • 利用数学的规律,可以通过计算每个位上数字9出现的次数来计算总次数。
    public class CountNinesInRange {
        public static int countNinesInRange(int n) {
            int count = 0;
            int multiplier = 1;
            int remainder = 0;
    
            while (n > 0) {
                int currentDigit = n % 10;
                n /= 10;
    
                count += n * multiplier;
                if (currentDigit > 9) {
                    count += multiplier;
                } else if (currentDigit == 9) {
                    count += remainder + 1;
                }
    
                remainder = currentDigit * multiplier + remainder;
                multiplier *= 10;
            }
    
            return count;
        }
    
        public static void main(String[] args) {
            int n = 100;
            int result = countNinesInRange(n);
            System.out.println("在1到" + n + "之间,数字9出现的总次数为: " + result);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    优点:

    • 效率较高,不需要遍历每个数字。

    缺点:

    • 需要了解数学规律,不适用于所有数字统计问题。

    总结:
    在这种情况下,方法3(数学法)通常是最好的选择。它效率较高,不需要遍历所有数字,而是利用数学规律直接计算结果。方法1和方法2适用于小范围的数字,但在大范围的情况下可能效率较低。然而,对于更复杂的数字统计问题,方法1和方法2可能更易理解和实现。因此,最佳方法取决于具体问题的要求和数据规模。

  • 相关阅读:
    离线数仓 (十二) --------- DIM 层搭建
    Python:实现djb2哈希算法(附完整源码)
    Http状态码
    运动装备品牌排行榜,运动爱好者必备好物分享
    【PID优化】基于樽海鞘算法PID控制器优化设计含Matlab源码
    上海亚商投顾:三大指数小幅下跌 CPO、算力板块集体爆发
    Julia编程基础
    前端数字计算精度问题
    【自然语言处理概述】文本词频分析
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景
  • 原文地址:https://blog.csdn.net/2302_79769114/article/details/133782354