数字统计(LeetCode Number of digit One)

1. 
   
 一本书的页码从自然数1开始顺序编码直到自然数n。书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0。例如第6页用6表示而不是06或
006。数字统计问题要求对给定书的总页码,计算出书的全部页码中分别用到多少次数字0,1,2,3,.....9。

思路:对于n位数,例如3位数,000-999,出现0,1,2...9的次数相同,记每个数字出现的次数为avg,这10个数字出现的总次数为sum=10*avg。

而n位数共有10^n个数,每个具体的数对于sum的贡献为n次。

得到:10*avg=n*10^n,

解出   avg=n*10^(n-1)。

2.

比如,对于一个数字34567,我们可以这样来计算从1到34567之间所有数字中每个数字出现的次数:
从0到9999,这个区间的每个数字的出现次数可以使用原著中给出的递推公式,即每个数字出现4000次。

从10000到19999,中间除去万位的1不算,又是一个从0000到9999的排列,这样的话,从0到34567之间

的这样的区间共有3个。所以从00000到29999之间除万位外每个数字出现次数为3*4000次。然后再统计

万位数字,每个区间长度为10000,所以0,1,2在万位上各出现10000次。而3则出现4567+1=4568次。

之后,抛掉万位数字,对于4567,再使用上面的方法计算,一直计算到个位即可。

3.

 1 // advance example
 2 #include <iostream>     // std::cout
 3 #include <iterator>     // std::advance
 4 #include <list>         // std::list
 5 #include <cmath>
 6
 7  void statNumber(int n) {
 8     int m, i, j, k, t, x, len = (int)log10(double(n));//*****调用函数求数字位数,机智 *****
 9     char d[16];
10     int pow10[12] = {1}, count[10] = {0};
11    // cout<<pow10[2]<<endl;
12     for(i = 1; i < 12; i++) {
13     pow10[i] = pow10[i-1] * 10;
14     }
15     sprintf(d, "%d", n);//example:n=9876,   d[0]=9,d[1]=8,d[2]=7,d[3]=6,d[4..15]=[]
16    for( i=0;i<5;++i)
17    {
18     putchar(d[i]);
19     putchar(‘\n‘);
20 }
21     m = n+1;//!!!
22     for(i = 0; i <= len; i++) {
23     x = d[i] - ‘0‘;
24     t = (m-1) / pow10[len-i];
25
26     count[x] += m - t * pow10[len-i];
27
28     t /= 10;
29     j = 0;
30     while(j <= x-1) {
31         count[j] += (t + 1) * pow10[len-i];
32         j++;
33     }
34     while(j < 10) {
35         count[j] += t * pow10[len - i];
36         j++;
37     }
38     count[0] -= pow10[len-i]; /* 第i个数位上前10^i个0是无意义的 */
39     }
40     std::cout<<"number"<<"    "<<"count"<<std::endl;
41     for(j = 0; j < 10; j++) {
42     std::cout<<j<<"    ";
43     printf("     %d\n", count[j]);
44     }
45 }
46 int main()
47 {
48     int a=1211;
49     std::cout<<"the number of pages is:"<<a<<std::endl;
50
51     statNumber(a);
52     system("PAUSE");
53     return 1;
54
55     } 

时间: 2024-11-08 21:29:20

数字统计(LeetCode Number of digit One)的相关文章

[LeetCode] Number of Digit One 数字1的个数

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

Leetcode: Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

[LeetCode]Number of Digit One,解题报告

题目 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 解题思路 题目理解: 题目的意思是给定一个数

LeetCode OJ 之 Number of Digit One (数字1的个数)

题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 思路: 对这个数字的每一位求存在1的数字的

[LeetCode]67. Number of Digit One1的个数和

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow. S

233. Number of Digit One(统计1出现的次数)

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 按不同位置统计 31456 统计百位时: (0-31)

[Swift]LeetCode233. 数字1的个数 | Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. Example: Input: 13 Output: 6 Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 给定一个整数 n,计算所有小于等于 n 的非负整数中数字

LeetCode 233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个

233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow. c