Evaluate the number where 1 appears in the numbers from 1 to n

Source: Internet
Author: User

Method 1: Enumerate all numbers from 1 to n. Each number calculates the number of 1 s. The result is obtained after the sum. The complexity is O (n * lnn)

Method 2: calculate the total number of occurrences by the number of occurrences of the last bit. For example, 512,

The numbers shown in 1 include:

1 (1)

11, 21, 31,... 91 (9)

101,121,131,... 191 (10)

201,211,221 .. 291 (10)

301,311,321,... 391 (10)

401,411,421,... 491 (10)

501,511, (2)

You contributed 52 (51 + 1) X 1

The number of contributions of 10 to 1 is:

10, 11, 12,... 19 (10)

110,111,... 119 (10)

210,211 .. 219 (10)

310,311,... 319 (10)

410,411,... 419 (10)

510,511,512

(5) * 10 + 2 + 1

The number of contributions of one hundred bits is:

100, 199 (100)

(0 + 1) x 100

Through continuous analysis, we can find a rule:

If the P bit is 0, the contribution of this bit is a high number * 10 ^ P;

If P is 1, the result of contribution of this bit is a high number * 10 ^ P + low number + 1

If the P bit is greater than 1, the result of this bit contribution is (high digit + 1) * 10 ^ P

ThisAlgorithmIt is only related to the length of the input number. The length of the input number is lnn, so the complexity is lnn.

Based on the above analysisProgramAs follows:

# Include <stdio. h> int count1 (INT value) {int num = 0; while (value) {If (Value % 10 = 1) {num ++ ;} value = value/10;} return num;} int power (INT Radix, int exp) {int result = 1; for (INT I = 0; I <exp; ++ I) {result * = Radix;} return result;} int count1_1 (INT value) {int current_number = 0; int higher_parts = 0; int lower_parts = 0; int Radix = 1; int num = 0; while (value/Radix) {current_number = (value/Radix) % 10; lower_parts = value-(value/Radix) * Radix; higher_parts = value/(Radix * 10); Switch (current_number) {Case 0: num + = higher_parts * Radix; break; Case 1: num + = higher_parts * Radix + lower_parts + 1; break; default: num + = (higher_parts + 1) * Radix;} Radix * = 10;} return num ;} int main (INT argc, char ** argv) {const int kvalue = 512; int num = 0; For (INT I = 0; I <= kvalue; ++ I) {num + = count1 (I);} printf ("% d \ n", num); printf ("% d \ n", count1_1 (kvalue ));}

References:

Http://www.nowamagic.net/algorithm/algorithm_CountOccurrencesOfOne.php

Programming beauty 2.4

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.