The beauty of programming-Chapter 2-the charm of numbers-2.1-calculate the number of 1 in the binary number-answers to after-school extension questions
1> if it becomes a 32-bit DWORD, it is obvious that solution 4 and solution 5 cannot meet the requirements, because such a large array space cannot be opened and so many numbers cannot be listed manually, let's look at solution 1 and solution 2. the time complexity of the algorithm has not changed. It can also be solved perfectly. In solution 3, the time complexity is still only related to the number of 1, is the optimal solution 2> how many digits are different in the binary representation of integers A and B? Here, we first think that if they are different, we can use an exclusive or operation, and then the problem is converted to finding the number of 1 in a binary number. solution 3 can be used to quickly calculate the number.
Magic of numbers-2.1