Interview Questions (67): 1 .... Number of occurrences of '1' in N

Source: Internet
Author: User

[Problem description]
(1) For an integer N, write a function f (n) so that it returns the number of '1' in all natural numbers between 1 and N (expressed in decimal number. For example, for n = 13, natural numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, '1' appears 6 times, so f (13) = 6.
(2) Find the maximum N value that satisfies f (n) = n.

[Problem Analysis]
This question is discussed in the beauty of programming. Although the correct answer is provided, the answer process is not rigorous. In addition, the efficiency of solving the second problem is too low.

This issue has been discussed in the C/C ++ version of Cu and has been well answered by netizens. Among them, the most effective solution is the algorithm provided by the netizen yzc2002, and provides a rigorous proof process. The following figure shows the analysis process:

In the above discussion, F (10 ^ k-1) = K * 10 ^ (k-1) is a very important conclusion.
For the last step (f (n) <n) of the above analysis, when M = N + (n-F (N)/K, F (m) is still satisfied) <m, so the next number to be verified can be changed to N + (n-F (N)/k + 1.

The implementation code is as follows (the Code modified from yzc2002 ):
# Include <stdio. h>

Long long F (long N)
{
Int K;
Long long e, R, M;

If (n <= 1)
Return N;

For (k = 0, E = 1, m = N; m> = 10; ++ K, E * = 10)
M/= 10;
R = N-M * E;

Return M = 1? (E/10 * k + R + 1 + f (R): (E/10 * K * m + E + f (R ));
}

Int digit_num (long N)
{
Int K;
For (k = 0; n> 0; ++ K)
N/= 10;
Return K;
}

Int main ()
{
Int COUNT = 0;
Long long N;

For (n = 1; n <0000000000ll ;)
{
Long long v = f (N );
If (V = N ){
Printf ("% LLD/N", N );
++ N;
++ Count;
}
Else if (V> N)
N = V;
Else {
Int K = digit_num (n + N-V );
N + = (n-v)/k + 1;
}
}

Printf ("Total: % d/N", count );

Return 0;
}

The running result is as follows:
-Bash-3.00 $ time./A. Out
1
199981
199982
199983
199984
199985
199986
199987
199988
199989
199990
200000
200001
1599981
1599982
1599983
1599984
1599985
1599986
1599987
1599988
1599989
1599990
2600000
2600001
13199998
35000000
35000001
35199981
35199982
35199983
35199984
35199985
35199986
35199987
35199988
35199989
35199990
35200000
35200001
117463825
500000000
500000001
500199981
500199982
500199983
500199984
500199985
500199986
500199987
500199988
500199989
500199990
500200000
500200001
501599981
501599982
501599983
501599984
501599985
501599986
501599987
501599988
501599989
501599990
502600000
502600001
513199998
535000000
535000001
535199981
535199982
535199983
535199984
535199985
535199986
535199987
535199988
535199989
535199990
535200000
535200001
1111111110
Total: 83

Real 0m0. 026 s
User 0m0. 016 s
Sys 0m0. 010 s

For the implementation of the f function, because it is a tail recursion, it is easy to change to a non-recursive version:
Long long F2 (long N)
{
Int K;
Long long e, R, M;
Long long result = 0;

While (n> 0 ){
For (k = 0, E = 1, m = N; m> = 10; ++ K, E * = 10)
M/= 10;
R = N-M * E;
Result + = E/10 * K * m + (M = 1? R + 1: E );
N = R;
}

Return result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.