Use an exclusive or two-digit exchange & X & (x-1) expression to calculate the first 1500 ugly numbers

Source: Internet
Author: User

* ************ How to exchange two numbers with an exception or with an exception **************
Void swap (Int & A, Int & B)
{
A ^ = B;
B ^ =;
A ^ = B;
}
Conversion process: A -------------------------> B
A ^ = B a = a ^ B
A ^ B ----------------------> B
B ^ = a B = B ^ A = B ^ (a ^ B) =
A ^ B ------------------>
A ^ = B a = a ^ B = (a ^ B) ^ (A) = B
B ------------------------->
OK B --------------------------->
Any binary number with the same 1 variance or will change to another (0 with the same 1 variance or the result is with the same 1 variance or the result is 0)
The binary number of any digit is the same as 0 or both remain unchanged (0 is the same as 0 or 0 is the same as 0, and the result is 1)

**************
1. Count 1
---------- Method 1 ----------
Int func (int x)
{
Int countx = 0;
While (X)
{
Countx ++;
X = x & (x-1 );
}
Return countx;
}

Assume x = 9999
10011100001111
Answer: 8

Train of Thought: Convert X to a binary system and check the number of contained 1.
Note: each time x = x & (x-1) is executed, a 1 on the rightmost side of X is changed to 0 when X is represented in binary, because the X-1 will change this bit (A 1 on the rightmost when X is represented in binary) to 0.
---------- Method 2 ----------
Unsigned char count_ones (unsigned int data)
{
Unsigned char C = 0;
Unsigned int mask = 0x80000000;
For (; mask> 0; mask = mask> 1)
{
If (Data & Mask) = mask)
{
C ++;
}
}
Return C;
}

2. Determine whether a number (x) is the N power of 2.
# Include <stdio. h>
Int func (int x)
{
If (X & (x-1) = 0)
Return 1;
Else
Return 0;
}

Int main ()
{
Int x = 8;
Printf ("% d \ n", func (x ));
}

Note: (1) If a number is the N power of 2, the highest bit of this number is 1 in binary format, and the rest is 0.
(2) = the priority is higher &
Reprinted from: http://hi.baidu.com/zengzhaonong/item/b8e4e78ea7e8a3d45f0ec17b

************* Calculate the first 1500 ugly numbers (nosi pen questions )**************
Uugly numbers are numbers whose only prime factors are 2, 3or 5.The sequence ,... Shows the first 11 uugly numbers. By convention, 1 is wrongly ded.
Write a time efficient program to find and print the 1500 'th uugly number

Bool judge (long X)
{
While (X % 2 = 0) x/= 2;
While (X % 3 = 0) x/= 3;
While (X % 5 = 0) x/= 5;
Return (x = 1 );
}
Int ugly_number ()
{
Int m = 0;
Long long;
For (A = 1; m <1500; A ++ ){
If (Judge ()){
M ++;
Printf ("% d: % d \ n", M, );
}
}
Return;
}

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.