1.
The page number of a book is encoded sequentially from the natural number 1 until the natural number N. The page numbers of the books are arranged according to the usual custom, each page number does not contain the redundant leading digit 0. For example, page 6th is represented by 6 instead of 06 or 006. The problem of digital statistics requires the total number of pages in a given book to calculate how many times the number is used in each page of the book 0,1,2,3,..... 9.
Idea: for n digits, such as 3 digits, 000-999, the number of occurrences of 0,1,2...9 is the same, the number of times each number appears AVG, the total number of 10 digits is sum=10*avg.
N-digits have a total number of 10^n, and each specific number contributes n times to sum.
Get: 10*avg=n*10^n,
Solve the avg=n*10^ (n-1).
2.
For example, for a number 34567, we can calculate the number of occurrences of each number in all numbers from 1 to 34567:
From 0 to 9999, the number of occurrences of each number in this interval can be used in the original version of the recursive formula, that is, each number appears 4,000 times. From 10000 to 19999, the middle of the removal of the million-digit 1 does not count, is a 0000 to 9999 of the arrangement, so that from 0 to 34567 of such an interval total 3. So the number of occurrences of each number is 3*4000 times from 00000 to 29999, except for the million digits. Then the number of thousands of digits, each interval length of 10000, so the 0,1,2 on the million-bit appear 10,000 times. and 3 appears 4567+1=4568 times. After that, throw out the million digits, for 4567, and then use the above method to calculate, has been calculated to the single digit.
3.
1//Advance Example2 #include <iostream>//Std::cout3 #include <iterator>//Std::advance4 #include <list>//Std::list5 #include <cmath>67void Statnumber (IntN) {8int M, I, J, K, T, x, Len = (int) log10 (Double (n));//Call the function to find the number of digits, wit * * * *9Char d[16];10int pow10[12] = {1}, count[10] = {0};11//cout<<pow10[2]<<endl;12for (i =1; I <12; i++) {Pow10[i] = pow10[i-1] *10;14}sprintf (D,"%d", n);//example:n=9876, d[0]=9,d[1]=8,d[2]=7,d[3]=6,d[4..15]=[]16for (i=0;i<5;++I17{18Putchar (D[i]);Putchar (‘\ n‘);20}m = n+1;//!!!22for (i =0; I <= Len; i++) {x = D[i]-‘0‘;t = (M-1)/pow10[len-I];25COUNT[X] + = m-t * pow10[len-I];27T/=10;j =0;30while (J <= X1) {COUNT[J] + = (t +1) * pow10[len-I];J + +;33}34while (J <10) {COUNT[J] + = T * Pow10[len-I];J + +;37}count[0]-= pow10[len-i];/*The first number of digits forward 10^i 0 is meaningless*/39}std::cout<<"Number"<<""<<"Count"<<Std::endl;41for (j =0; J <10; J + +) {std::cout<<j<<"";printf ("%d\n", Count[j]);44}45}46IntMain ()47{48 int a=1211; " The number of pages Is:" <<a<<std::endl;< Span style= "color: #008080;" >50 51 Statnumber (a); pause); 53 return 1; 54 55}
Digital statistics (leetcode number of digit one)