56. 从1到n整数中1出现的次数

题目地址:https://www.acwing.com/problem/content/15/

AC代码

class Solution {
public:
    int NumberOf1Between1AndN_Solution(int n)
    {
        int count = 0;
        for (int i = 1; i <= n; i *= 10) {
            int a = n / i,b = n % i;
            count += (a + 8) / 10 * i + ((a % 10 == 1) ? b + 1 : 0);
        }
        return count;
    }
};

总结

答案参考:https://www.cnblogs.com/wmx24/p/8901808.html

你可能感兴趣的:(56. 从1到n整数中1出现的次数)