How many of the 0.1 billion numbers from 1 to 0.1 billion are 1?

Source: Internet
Author: User
At first glance, this question is really good for anyone. when the group sees this question, they are scrambling to give their views. The simplest way is to traverse each number, then toString () to see how many 1 s are in it, and finally add up all. this is the way we get the standard answer. Three people in the group wrote three stupid methods and ran out. The three stupid methods were both interesting and different. The implementation of the program is unpredictable.

At first glance, this question is really good for anyone. when the group sees this question, they are scrambling to give their views. The simplest way is to traverse each number, then toString () to see how many 1 s are in it, and finally add up all. this is the way we get the standard answer.

Three people in the group wrote three stupid methods and ran out. The three stupid methods were both interesting and different. The implementation of the program is unpredictable.

Var re =/1 {1}/g; var max = 1x10000*10000; getTotal (f); getTotal (f1); getTotal (f2); function getTotal (func) {var total = 0; var begin = new Date (); for (var I = 1; I <= max; I ++) {total + = func (I );} var end = new Date (); var timespan = end-begin; alert ("Start time:" + begin + "\ n end time: "+ end +" \ n total time consumption: "+ timespan +" millisecond \ n total: "+ total);} function f (num) {var t = 0; while (num) {if (num <10) {if (num = 1) t ++; break;} var I = num % 10; if (I = 1) t ++; num = parseInt (num/10);} return t;} function f1 (num) {var str = num. toString () var t = 0; for (var I = 0; I
         
  

When the number is small, the first is the fastest. Obviously, there is no step to process strings. The larger the number, the more advantageous it is. However, the test results are similar to those of the three. However, when running with C #, the first one is obviously more and more advantageous ....

However, this is definitely not the case for the Author. many people are talking about their skills and opinions. I have been thinking for a long time.

Take 100 for example. how many 1 s are contained in 100?

1011121314151617181901112131415161718191

We can arrange 0 ~ 99 is regarded as an array with a length of 2. when one bit is 1, the possibility of two bits is 10. when two bits are 1, the possibility of one bits is 10, so 0 ~ 99 should have 20 1, while 100 should have 1, so 21.

999 should be 1*10*10 + 10*1*10 + 10*10*10*1 = 300

What about 400? Because only 0 ~ 3, so it should be 1*10*10 + 4*1*10 + 4*1*10

4 0000 0000 should be 1*(10 ^ 8) + 4*(10 ^ 7) * 8 = 420000000

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1331,welcome.

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.