A Google Java interview question !!!
Java code:
- Consider a function which,ForA given whole number N, returns the number of ones required when writing out all numbers between 0 and N.
- For example, F (13) = 6. Notice that F (1) = 1. What is the next largest N such that F (n) = n?
The translation is basically like this:
There is an integer n that writes a function f (N) and returns the number of "1" between 0 and N. For example, F (13) = 6. Now F (1) = 1. Ask what is the next largest F (n) = n?
Answer 1:
- IntGetcountofnumber (IntNumber ){
- IntCount = 0;
- Int Length= ("" + Number ).Length();
- For(IntI = 0; I <=Length; I ++ ){
- IntNum = Number % 10;
- Number = (number-num)/10;
- If(Num * num = 1) Count ++;
- }
- ReturnCount;
- }
Calculated: 199981 used 203
However, it doesn't make much sense to calculate only the value above. Let's look at this:
This is the result within 4000000000! :
F (0) = 0
F (1) = 1
F (199981) = 199981
F (199982) = 199982
F (199983) = 199983
F (199984) = 199984
F (199985) = 199985
F (199986) = 199986
F (199987) = 199987
F (199988) = 199988
F (199989) = 199989
F (199990) = 199990
F (200000) = 200000
F (200001) = 200001
F (1599981) = 1599981
F (1599982) = 1599982
F (1599983) = 1599983
F (1599984) = 1599984
F (1599985) = 1599985
F (1599986) = 1599986
F (1599987) = 1599987
F (1599988) = 1599988
F (1599989) = 1599989
F (1599990) = 1599990
F (2600000) = 2600000
F (2600001) = 2600001
F (13199998) = 13199998
F (35000000) = 35000000
F (35000001) = 35000001
F (35199981) = 35199981
F (35199982) = 35199982
F (35199983) = 35199983
F (35199984) = 35199984
F (35199985) = 35199985
F (35199986) = 35199986
F (35199987) = 35199987
F (35199988) = 35199988
F (35199989) = 35199989
F (35199990) = 35199990
F (35200000) = 35200000
F (35200001) = 35200001
F (117463825) = 117463825
F (500000000) = 500000000
F (500000001) = 500000001
F (500199981) = 500199981
F (500199982) = 500199982
F (500199983) = 500199983
F (500199984) = 500199984
F (500199985) = 500199985
F (500199986) = 500199986
F (500199987) = 500199987
F (500199988) = 500199988
F (500199989) = 500199989
F (500199990) = 500199990
F (500200000) = 500200000
F (500200001) = 500200001
F (501599981) = 501599981
F (501599982) = 501599982
F (501599983) = 501599983
F (501599984) = 501599984
F (501599985) = 501599985
F (501599986) = 501599986
F (501599987) = 501599987
F (501599988) = 501599988
F (501599989) = 501599989
F (501599990) = 501599990
F (502600000) = 502600000
F (502600001) = 502600001
F (513199998) = 513199998
F (535000000) = 535000000
F (535000001) = 535000001
F (535199981) = 535199981
F (535199982) = 535199982
F (535199983) = 535199983
F (535199984) = 535199984
F (535199985) = 535199985
F (535199986) = 535199986
F (535199987) = 535199987
F (535199988) = 535199988
F (535199989) = 535199989
F (535199990) = 535199990
F (535200000) = 535200000
F (535200001) = 535200001
F (1111111110) = 1111111110
Someone wrote one in C, and it took only dozens of milliseconds to get these results!