C + + to find 1 to n 1 occurrences and number of binary representation of 1 of the number of _c language

Source: Internet
Author: User
Tags bitwise

Number of occurrences of 1 in positive numbers from 1 to n

Topic:

Enter an integer n to find the number of occurrences of 1 in the decimal representation of the n integers from 1 to n.

For example, input 12, from 1 to 12 of these integers contains 1 of the number 1, 10, 1 1 and 12, 11 of which have occurred 5 times

Code implementation (GCC compilation passed):

#include "stdio.h" #include "stdlib.h" int count1 (int n);
 
int count2 (int n);
 
  int main (void) {int x;
  printf ("Enter a number:");
  scanf ("%d", &x);
   
  printf ("\ n from 0 to%d total%d (%d) 1\n", X,count1 (x), Count2 (x));
return 0;
  //Solution one int count1 (int n) {int count = 0;
   
  int i,t;
    Traverse 1 to N for (i=1;i<=n;i++) {t=i;
      To process the individual bit while (T!= 0) of the currently traversed number, add a count of + + (t%10 = 1) for 1? 1:0;
    t/=10;
} return count; }//Solution two: int count2 (int n) {int count = 0;//statistic variable int factor = 1;//factorization factor int lower = 0;//all lower int higher for the current processing bit = 0;//all High level int Curr =0;//current processing bit (n/factor!= 0) {lower = n-n/factor*factor;//to get low Curr = (n/f Actor)%10;//to find the current bit higher = n/(FACTOR*10),//High position switch (curr) {case 0:count + = higher * FAC
        Tor
      Break
        Case 1:count + = higher * factor + lower + 1;
      Break
    Default:count + = (higher+1) *factor;
     
  }  Factor *= 10;
return count;

 }


Analysis:

The first method is to traverse to n from 1, each of which contains the number of "1" add up, better think.

Method Two is more interesting, the core idea is this: statistics on each one may appear 1 times.

Like 123:

Number of digits appearing 1:1,11,13,21,31,...,91,101,111,121

10 digits appearing in 1:10~19,110~119

Hundreds of 1 of digits appear: 100~123

To sum up the rules of each of the 1 appearing, we can get the method two. Its time complexity is O (len), Len is the number length

The number of 1 in the binary representation of integers
Title: Number of 1 in binary representation of integers

Requirements:

Enter an integer that asks how many 1 are in the binary expression of the integer.

For example, enter 10, because the binary representation is 1010, there are two 1, so output 2.

Analysis:

The solution one is the ordinary processing way, through besides two two statistics 1 number;

The solution two is similar to the solution, and is processed by the right displacement, each with the number of 1 bitwise AND statistic 1.

Solution Three is more wonderful, each time the last one of the number processing into 0, statistical processing times, and then statistics 1 of the number of

Code implementation (GCC compilation passed):

#include "stdio.h"
#include "stdlib.h"
 
int count1 (int x);
int count2 (int x);
int count3 (int x);
 
int main (void)
{
  int x;
  printf ("Enter a number: \ n");
  Setbuf (stdin,null);
  scanf ("%d", &x);
  printf ("%d into binary 1 of the number is:", x);
  printf ("\ n Solution One:%d", Count1 (x));
  printf ("\ n Solution II:%d", Count2 (x));
  printf ("\ n Solution three:%d", Count3 (x));
   
  printf ("\ n");
  return 0;
}
 
Every
int count1 (int x)
{
  int c=0 is
  counted in order of two; while (x)
  {
    if (x%2==1)
      C + +;
    x/=2;
  }
  return c;
}
 
Shift right, with 1 bitwise and statistic per bit
int count2 (int x)
{
  int c=0;
  while (x)
  {
    c+=x & 0x1;
    x>>=1;
  }
  return c;
}
 
Each time the last 1 is processed into 0, the statistical processing times
int count3 (int x)
{
  int c=0;
  while (x)
  {
    x&= (x-1);
    C + +;
  }
  return c;
}

Related Article

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.