4
头图

The number of occurrences of 1 in an integer from 1 to n

Title description

Find the number of occurrences of 1 in the integer of 1-13, and calculate the number of occurrences of 1 in the integer of 100-1300? For this reason, he specifically counted the numbers 1 to 13 that contain 1, there are 1, 10, 11, 12, and 13, so there are 6 times in total, but he has nothing to do with the latter problem. ACMer hopes that you can help him and make the problem more general. It can quickly find the number of occurrences of 1 in any non-negative integer interval (from 1 to the number of occurrences of 1 in n).

title link : from 1 to n, the number of occurrences of 1 in an integer

Code

/**
 * 标题:从 1 到 n 整数中 1 出现的次数
 * 题目描述
 * 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,
 * 但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。
 * 题目链接:
 * https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6?tpId=13&&tqId=11184&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz31 {

    public int numberOf1Between1AndN_Solution(int n) {
        int result = 0;
        for (int i = 1; i <= n; i++) {
            int num = i;
            while (num != 0) {
                if (num % 10 == 1) {
                    result++;
                }
                num = num / 10;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Jz31 jz31 = new Jz31();
        System.out.println(jz31.numberOf1Between1AndN_Solution(10));
    }
}
[Daily Message] Knowing how to be grateful is the source of happiness; knowing how to be grateful, you will find that everything around you is so beautiful.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!