Http://ac.nbutoj.com/Problem/view.xhtml? Id = 1475
Problem description
The hot summer training session is about to end. In this short 20 days, everyone is working very hard, because many of them are bachelors. Balabala
So marknoon has always been worried. After all, he is also single.
One day, marknoon looked at a string of numbers and found the number 1 connected to him. So he began to get bored and wanted to know from Number 1 to number N, A total of 1.
For example, if n = 12, the number of 1 is 5, and the numbers of 1 are, and 12 respectively.
Input
Enter N (1 <=n <= 2147483647 ).
Output
Output The number of 1 in all numbers from 1 to n.
Sample Input
313123
Sample output
1657
I have seen this question for the third time.
Principle: http://blog.csdn.net/qingniaofy/article/details/8764926
# Include <stdio. h> # include <math. h >__ int64 countone (_ int64 N) // The number of count 1 {_ int64 COUNT = 0; If (n = 0) Count = 0; else if (n> 1 & n <10) Count = 1; else {_ int64 highest = N; _ int64 bit = 0; while (highest> = 10) {highest = highest/10; bit ++;} _ int64 Weight = (_ int64) Pow (10, bit); If (highest = 1) {COUNT = countone (weight-1) + countone (n-weight) + N-weight + 1;} else {COUNT = highest * counton E (weight-1) + countone (n-highest * weight) + weight;} return count;} int main () {_ int64 N; while (~ Scanf ("% i64d", & N) {printf ("% i64d \ n", countone (N) ;}return 0 ;}