Problem Description:
The page number of a book begins with the number of natural numbers 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, the sixth page is represented by a number of 6, not 06 or 006, and the number counting problem requires a given total page number n, which calculates how many times the number is used in all the pages of the book 0,1,2,........... 9.
Programming Task: Decimal integer n (1<=n<=10^9) for the total page number of the given book. How many times 0,1,2,3 are used in all the page numbers of the programming calculation book,.............. 9
/********************************************************************** * Copyright (c) 2015,wk Studios * Filename: * compiler:gcc,vs,vc6.0 Win32 * AUTHOR:WK * time:2015 4 2 ****************************************************** ///1.<span style= "Font-family:microsoft Yahei;" > First algorithm thought </span><span style= "Font-family:microsoft Yahei;" >//</span> Code time Complexity O (N*LOG10 (n)),//for M input integer calculation, the time required is 1*log10 (1) + 2*LOG10 (2) + ... + m*log10 (m),//According to function f (x) = X*LOG10 (x), when n > 10 o'clock N*LOG10 (n) >n when n<=10 n*log10 (n) <n///So for m integer inputs can be approximately 1+2+3+.......+m approximate m*m for the lower bound of the function/for 300,000 data run time is 9*10^10 #include <iostream>using namespace std;void count_num (int n) {int count[10]={0};for (int i=0;i& lt;=n;i++) {int t=i;if (0==t) {count[0]++;//calculates the number of the first 0}while (t) {count[t%10]++;//calculates the number of high occurrences t/=10;//sequentially to the low}}for (i=0;i <10;i++) {cout<< "Present num of" <<i<< "is" <<count[i]<< ' \ n ';}} int main () {count_num (34567); return 0;}
Algorithm 1-Statistical problems