Prove safety offer31: integer number 1 appears (the number of occurrences n is an integer from 1 to 1)

1 Title Description

  Calculated the number of integer of 1 to 13 1 arise, and calculates the number of times an integer from 100 to 1300 in 1 occur? To this end he especially counted about 1 to 13 contains the digits 1 has 1,10,11,12,13 therefore appear a total of six times, but for the problem behind him Meizhe. ACMer hope you will help him, and the problem is more generalized, the number of non-negative integer in the range 1 appears (the number of occurrences from 1 to n. 1) can quickly find any.

2 ideas and methods

  Count the number of times each integer n decimal digits 1 arise, no matter how tired together. For each bit can be split into a number of front (begin) + intermediate (middle) + rear (end). One hundred 1234 + 2 + 1 = 34 

3 C ++ core code

Simple version: https: //blog.csdn.net/typantk/article/details/88386888 (explain too good)

 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 };
View Code

Code more

. 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          the while (TEMP) {
 10              Last = TEMP% 10 ;      // digits whether. 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 };
View Code

4. C ++ code for a complete

 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 }
View Code

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

Reference material

https://blog.csdn.net/typantk/article/details/88386888 (explain too good)

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

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11415964.html