Offer 31: the number of occurrences of 1 in integers (from 1 to 1 in n integers)

Keywords: C++

1 Topic Description

Find out the number of occurrences of 1 in integers 1-13 and the number of occurrences of 1 in integers 100-1300. For this reason, he counted the numbers 1, 10, 11, 12, 13 contained in 1 to 13, so there were six times, but he had no problem with the latter. ACMer wants you to help him and make the problem more general, so that you can quickly find the number of times 1 occurs in any nonnegative integer interval (from 1 to 1 in n).

2 Thoughts and Methods

Count the number of occurrences of 1 in each decimal bit of the integer n, and then add them up. For each individual, a number can be divided into first + middle + end. The hundred digits of 1234 = 1+2+34

3 C++ Core Code

Concise version: https://blog.csdn.net/typantk/article/details/88386888 (excellent explanation)

 1 class Solution {
 2 public:
 3     int NumberOf1Between1AndN_Solution(int n)
 4     {
 5         int ones = 0;
 6         for (long m = 1; m <= n; m *= 10)
 7             ones += (n/m + 8) / 10 * m + (n/m % 10 == 1 ? n%m + 1 : 0);
 8         return ones;
 9     }
10 };

More code

 1 class Solution {
 2 public:
 3     int NumberOf1Between1AndN_Solution(int n)
 4     {
 5         int temp = n;
 6         int last;
 7         int result = 0;
 8         int base = 1;
 9         while(temp){
10             last = temp%10;     //Is the number of digits 1
11             temp = temp/10;    //Remove digits
12             result += temp*base;
13             if (last==1){
14                 result += n%base + 1;
15             }
16             else if(last>1){
17                 result += base;
18             }
19             base *= 10;
20         }
21         return result;
22     }
23 };

4. C++ Complete Code

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 long long fun(long long n)
 6 {
 7     if (n < 1)
 8         return 0;
 9     long long count = 10, num = 0, begin, middle, end, m;
10     begin = n;
11     middle = 0;
12     end = 0;
13     while (begin)
14     {
15         begin = n / count;
16         m = n%count;
17         middle = m / (count / 10);
18         end = m % (count / 10);
19         if (middle > 1)
20             num = num + (count / 10) * (begin + 1);
21         else if (middle == 1)
22             num = num + (count / 10) * begin + (end + 1);
23         else
24             num = num + (count / 10) * begin;
25         count = count * 10;
26     }
27     return num;
28 }
29 
30 int main()
31 {
32     long long n, m;
33     while (scanf("%lld %lld", &n, &m) != EOF)
34     {
35         if (n>m)
36             printf("%lld\n", fun(n) - fun(m - 1));
37         else
38             printf("%lld\n", fun(m) - fun(n - 1));
39     }
40     printf("%\n");
41 
42     system("pause");
43     return 0;
44 }

https://blog.csdn.net/zhoubin1992/article/details/47361969

Reference material

https://blog.csdn.net/typantk/article/details/88386888 (excellent explanation)

https://blog.csdn.net/u012477435/article/details/83351659#_873

Posted by MastahUK on Sat, 05 Oct 2019 01:04:19 -0700