Blue Bridge Cup training algorithm represents the power of two

Bloggers in recent brush Blue Bridge Cup official website of the title, found an interesting algorithm problem:
the title of class content as follows:

Problem description
  any positive integer can be represented in binary, for example: 137 is represented as 10001001 binary.
  This represents a binary power of 2, written in the form and, so that the top surface of the high-power, gives the following expression: 137 = 2 7 + 2 3 + 2 ^ 0
  Now agreed with powers of brackets He indicates that a ^ b is expressed as a (b)
  In this case, 137 may be expressed as: 2 (7) + 2 (3) + 2 (0)
  further: 7 = 2 2 + 2 + 2 0 (2 ^ 1 with 2 represents a)
  3 + 2 ^ 2 = 0
  so finally 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 + 2 + 1 ^ 5
  it can be expressed as the final 1315:
  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
  -conforming n represents 0,2 (no spaces in the representation)
input sample
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)

Subject to the effect of it is so, the question is very easy to understand, is to implement the amount of ...... Well,
all right when I did this, I talk about ideas to get started on this question when it first thought is nested loop, plus if the judge, but you want a good practice will find that this is tantamount to breaks against, your thoughts will be with these layers of nested gradually stir into a ray of mess (~% ?? ..., # * '☆ & ℃ $ ︿ ★)
later studying smart bloggers, coupled with the recent like to use the binary bit operation to solve the problem:
I changed my idea of recursive methods to achieve the purpose of this question:

Well in fact are recursive recursive routines (the same years of template):
1, the first consideration is recursive exports (and best thinking):
maybe about thinking we can certainly think if we get a number of recursive function n = 10
is first converted into binary format (binary conversion method on the blogger explained here too the previous article is not a process of converting Aberdeen)
10 = "(binary) 1010
1010 and can be understood as 2 ^ 3 + 2 + 0 ^ 1 + 0
----------------------------- 1-0. 3 ------ ----- 2
does not we know we found that the law did not think this logic is that from the beginning until the third highest last 0 end
right here, 0 is recursive exports;
that our program will have:
Here Insert Picture Description
when we are on n == 0 from function
2, the next step is to understand a bit more difficult step:
we all know c ++ in an int is four bytes is 32 (4 * 8) bit int then zero in a manner that is stored in the computer 00000000000000000000000000000000 32 is zero because it is a signed type of representation that is the highest symbol of positive and negative relationship
I N want to get the highest bit is the first of several only need to use bitwise operators
we first moved to 1 << 31 31 highest to int n that Bitwise and (&)
10000000000000000000000000000000 00000000000000000000000000001010 & = 0
...... ......
...... ... ...
Until moving to third place 1 << 3;
00000000000000000000000000001000 & 00000000000000000000000000001010 = 1
we get the highest bit 10 of the third then it can be expressed as 2 (3) Since 3> 2 so we have to call the function itself nested again 3 will be passed to the function;

............ sequentially until n == 0 then the function stack inside space (solution) function to jump one level:
Here Insert Picture Description
3, and finally to be noted that the + sign is determined a number of issues such as if we 8 = "(binary ) 1000 now points to the highest level is our j = 3 so how do you determine whether to add back the + sign;
here you can use an ingenious method or by bit operation to complete
(binary) 1000 we take a full position to move to the left he performed with the same bit by bit until the number on the back also have to see if it's +
65535 (binary) 1111111111111111 (16 one-) were left (16-j) bits
so that 65535 >> (16-3) = 0000000000001111
0000000000000111 & 1000 = 0 is false, says all zeros behind it without adding the +
or on the increase
Here Insert Picture Description

The entire source code:

#include <iostream>
using namespace std;
void change(int n){
    if(n == 0){
        cout << 0;return;
    }
    else{
        int i = sizeof(int)*8 - 1;
        while(!(n&(1<<i))){
            i--;
        }
        for(int j = i;j>=0;j--){
            if(n&(1<<j)){
                if(j == 1){
                    cout << "2";
                }
                else{
                    cout << "2" << "(";
                    change(j);
                    cout << ")";
                }
                if(n&(65535>>(16-j))){
                    cout << "+";
                }
            }
        }
    }
}

int main(){
    int n;
    cin >> n;
    change(n);
}
Published 27 original articles · won praise 62 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42359956/article/details/87704205