The problem describes that any positive integer can be represented by a 2 binary, for example: 137 of the 2 binary is represented as 10001001.
The 2 notation is written in the form of the sum of the power of 2, and the second power is preceded by the following expression: 137=2^7+2^3+2^0
The Covenant power is now represented by parentheses, which means that a^b is represented as a (b)
At this point, 137 can be represented 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 = (no spaces in the representation) sample input 137 sample Output 2 (2 (2) +2+2 (0)) +2 (0) +2 Example input 1315 sample output 2 (2 (+2 (0)) +2) 2 (0 (+ +2) 2 (2)) +2+2 (0) Prompt with recursive implementation will be relatively simple, can be recursive side of the output of the following solutions reference from: http://www.2cto.com/kf/201501/368981.html
The idea of solving problems is realized by recursion. Convert the decimal number to binary, recording the conversion process as 1 is the first several cycles, and then judge. The boundary of recursion is when n==0,n==1n==2. But it should be noted that when the output + number, when not output. When the output + definition array is not the last one, it is defined as a local variable, because each number of groups is stored differently. AC Code
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;intMain () {intN; voidInitintN); while(~SCANF ("%d", &N)) {init (n); printf ("\ n"); } return 0;}voidInitintN) { intnum; intI, j, K; inta[ +];//arrays are defined as local variablesnum =0; I=0; while(n) {J= n%2; //memset (A, 0, sizeof (a)); if(J = =1) {A[num+ +] = i;//Store the first few times is 1} i++; N/=2; } for(i = num-1; I >=0; i--) { if(A[i] = =0) printf ("2 (0)"); Else if(A[i] = =1) printf ("2"); Else if(A[i] = =2) printf ("2 (2)"); Else if(A[i] >2) {printf ("2 ("); Init (A[i]); printf (")"); } if(I! =0) printf ("+"); }}
View Code
Recurses Representation of Algorithm Training 2 (recursive)