Topic details
A description of the problem no matter what a positive integer can be expressed in 2 notation, for example: 137 of the 2 binary is represented as 10001001.
This 2 notation is written in the form of the sum of the power of 2, which is preceded by the second power, and can be obtained such as the following expression: 137=2^7+2^3+2^0
now the Covenant power is expressed in parentheses, that is, a^b is represented as a (b)
At this time 137 can be expressed as: 2 (7) +2 (3) +2 (0)
Further: 7=2^2+2+2^0 (2^1 with 2)
3=2+2^0
So the last 137 can be represented as: 2 (2 (2) +2+2 (0)) +2 (+ 0) +2 (0)
Another example: 1315=2^10+2^8+2^5+2+1
So the last 1315 can be expressed as:
2 (2 (+ 0) +2) +2 (2 (+ + (0))) +2 (2 (2) +2 (0)) +2+2 (0) The input format positive integer (1<=n<=20000) output format conforms to the contract N of 0, 2 = (cannot have spaces in the representation) example Input 137 example output 2 (2 (2) +2+2 (0)) +2 (+ 0) Example input +2 example output 0 (1315 (+ 2) 2) 0 (+2 (+ +2) 2 (0)) +2+2 (0)
Tips
Recursive implementations are simpler. Can output on one side of the recursion side
Idea: 1, first convert the input 10 number into 2 binary number
2, the binary array bitwise processing, the original decimal number is represented as the corresponding binary representation (such as:137=2^7+2^3+2^0), pay attention to the red flag content
3, the inferred index can continue to be expressed as binary. It's the recursive title.
Note: 1, binary array from the back forward output is the original decimal binary form, so processing time to start from the last.
2, ^ converted into (). To add output control to (), the 2^1 outputs 2 directly, while for 2^0 and 2^2 outputs 2 (0) and 2 (2) respectively.
3, for the control of the plus. The last one does not need to output a plus sign after processing, starting from the beginning of a 0. This bit is processed without the need to output a plus sign.
Code:
#include <stdio.h>void solve (int n) {//Recursive exit//when exponent is 0 or 2 o'clock, direct output exponent if (n==0 | | n==2) {printf ("%d", n); return;} When the exponent is 1 o'clock (that is, 2 (1) does not output) if (n==1) {return;}/******* above is the recursive exit ********/char Str[20];int i=0,j,len,q;//is converted to 2 binary. The resulting string is arranged in the original flashback sequence while (n) {str[i]=n%2+ ' 0 '; n/=2; i++;} len = i;/*while () is used to indicate that the countdown is not 0. For controlling the output of the + sign */q=0; while (str[q]== ' 0 ') {q++;} /* for 2 binary string bitwise processing */////////variable J to indicate the current processing is the number of the output */for (i--, j=1;i>=0;i--, j + +) {if (str[i]!= ' 0 ') {/* If the character is currently being processed after another (the bit is 2 (1), */if ((len-j) ==1) {printf ("2");} Else{printf ("2 (");} /* Recursively handle */solve (LEN-J);/* If the character is currently being processed, another (the bit is 2 (1). */if ((len-j) ==1); else{printf (")");} /* The last one is currently processed, or the back of the current bit is all 0. No output +*/if ((len-j) ==0 | | i==q); else{printf ("+");}}} int main () {int n;scanf ("%d", &n),//1 handles if (n==1) {printf ("2 (0)") separately;} Else{solve (n);} return 0;}
South Bridge--recurses expression of algorithm training 2