N power of 2

Source: Internet
Author: User
Problem description

Any positive integer can be expressed in binary notation. For example, the binary notation of 137 is 10001001.
This binary representation is written as the sum of the power of the second, so that the power of the second is ranked first. The following expression can be obtained: 137 = 2 ^ 7 + 2 ^ 3 + 2 ^ 0
The power is represented by parentheses, that is, a ^ B is represented as a (B)
In this case, 137 can be expressed as: 2 (7) + 2 (3) + 2 (0)
Further: 7 = 2 ^ 2 + 2 + 2 ^ 0 (2 ^ 1 is expressed as 2)
3 = 2 + 2 ^ 0
So the last 137 can be expressed as: 2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)
Another example: 1315 = 2 ^ 10 + 2 ^ 8 + 2 ^ 5 + 2 + 1
So 1315 can be expressed:
2 (2 (2 + 2 (0) + 2) + 2 (2 (2 + 2 (0) + 2 (2 (2) + 2 (0 )) + 2 + 2 (0)
Input Format
Positive Integer (1 <= n <= 20000)
Output Format
0, 2 of the N that meets the conventions (there cannot be spaces in the representation)
Sample Input
137
Sample output
2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)
Sample Input
1315
Sample output
2 (2 (2 + 2 (0) + 2) + 2 (2 (2 + 2 (0) + 2 (2 (2) + 2 (0 )) + 2 + 2 (0)

Ideas

If it is simple, we can honestly calculate the binary and then output it. here we need to break down each binary index together, that is, we need to break down the number on the way, obviously recursion.

However, you do not need to calculate the binary data. bitwise operations are used to directly operate the binary data. Here, we use 137 for demonstration. The details are as follows:

First, we know that the binary value of 137 is 10001001.

1. obtain the highest binary
1 for(e=0, now=1; now <= n; now<<=1, e++);

 

Here, e Indicates the position of the current binary, that is, the index we need. The Initialization is 0 bits; now indicates the N power of 2, which is used to compare with the number n we input, determine the number of digits to move. Here we should look like this after the loop ends:

010001001 original 137

100000000 2 to the power of 256

At this time, E = 8, now = 256

2. Determine whether the current BIT is 0

This is probably the most exciting part.

1 For (; now> 0; now >>= 1, E --) {2 if (now & N ){
// Continue 3}

 

We already know the binary values of now and N. Next, when the loop condition is met, we will continue to shift the binary value to the left (E --), and change the bitwise and of now and N, the following result is displayed:

010001001 original 137

100000000 2 to the power of 256

000000000 0

If the condition is false, continue the next cycle:

010001001 original 137

010000000 2, 128 to the power of 7, because we shifted one to the right

010000000 at this time, it is not 0

If the condition is true, execute the following statement until the current binary is read.

The result of the next loop is

010001001 original 137

001000000 2 power 6 64

000000000 at this time, it is 0

Keep going ......

3. Determine the output
1 void fun (int n) 2 {3 int now, E; 4 for (E = 0, now = 1; now <= N; now <= 1, E ++); 5 cout <"2"; 6 for (; now> 0; now >>= 1, E --) {7 if (now & N) {8 If (now> 2) {// now is greater than 2, indicating that the index is greater than 2 and 9 cout <"; 10 fun (E ); // recursion 11 cout <")"; 12} 13 if (now = 2 ); // when the question is equal to 2, we do nothing. 14 else if (now = 1) {// if the question is equal to 1, we need to output the index 015 cout <"(0 )"; 16} 17/* 18 when the current index is decomposed, use the previous number to subtract the index currently decomposed, for example, 137 = 2 ^ 7 + 2 ^ 3 + 2 ^ 0. After 2 ^ 7 is decomposed, continue to break down 20 2 ^ 3 + 2 ^ 021 */22 N-= now; 23 if (n) {// Of course, if you can continue to break down, otherwise, the condition here is false 24 cout <"+ 2"; 25} 26} 27} 28}

 

Complete code
 1 #include<iostream>  2  3 using namespace std; 4  5 void fun(int n) 6 { 7     int now, e; 8     for(e=0, now=1; now <= n; now<<=1, e++); 9     cout<<"2";10     for(;now>0;now>>=1, e--){11         if(now&n){12             if(now > 2){13                 cout<<"(";14                 fun(e);15                 cout<<")";16             }17             if(now == 2);18             else if(now == 1){19                 cout<<"(0)";20             }21             n -= now;22             if(n){23                 cout<<"+2";24             }25         }26     }27 }28 int main()29 {30     int n = 0;31     cin>>n;32     fun(n);33     34     return 0;35 }
View code

 

 

N power of 2

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.