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 output on the side of recursion
#include <iostream>#include<cstdio>intTotwo (intNChara[]) { intlen=0; while(n!=0) {A[len++]=n%2+'0'; N/=2; } returnLen;}voidRecursion (intN) { Chara[ -]; intlen=Totwo (n,a); intcou=0; for(intI=0; i<len;i++){ if(a[i]=='1') {cou++; } } intj=0; for(inti=len-1; i>=0; i--){ if(j==0){ if(a[i]=='1'){ if(i==1) {printf ("2"); }Else if(i==0) {printf ("+2 (0)"); }Else{printf ("2 ("); recursion (i); printf (")"); } } }Else{ if(a[i]=='1'){ if(i==1) {printf ("+2"); }Else if(i==0) {printf ("+2 (0)"); }Else{printf ("+2 ("); recursion (i); printf (")"); } }} J++; } }intMainintargcChar**argv) { intN; while(~SCANF ("%d",&N)) {recursion (n); printf ("\ n"); } return 0;}
The recurses representation of algorithm training 2