Title Description
Any positive integer can be represented by a power of 2.
Example: 137=2^7+2^3+2^0
At the same time, it is agreed that the square is denoted by parentheses, that is, a^b can be represented as a (b)
The 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 1315 can be represented as: 2 (2 (+ + (0)) +2) +2 (2 (+ + (0)) +2 (2 (2) +2 (0)) +2+2 (0)
Input
Multiple groups, one positive integer per line (0<n<=20000)
Output
Each group of rows, conforming to the 0,2 representation of n (cannot have spaces in the representation)
--body recursion solves the problem of first turning a number into a binary sub-system to solve the power of 1 power times greater than 2 recursion that power is 2 or 0
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineSIZE 20intSolve (intN) { intk[ -]; intTotal =0, temp =N; while(Temp >1) { Total++; K[total]= temp%2; Temp/=2; } if(temp = =1) { Total++; K[total] =1; } intLast ; inti; for(i=1; i<=total;i++){ if(K[i] = =1) { last=i; Break; } } for(i=total;i>=1; i--){ if(K[i] = =0)Continue; intnow = i1; if(now = =1) {printf ("2"); } Else{printf ("2 ("); if(Now >=3) Solve (now); Elseprintf"%d", now); printf (")"); } if(I! =Last ) {printf ("+"); } }}intMain () {intN; while(SCANF ("%d", &n)! =EOF) {Solve (n); } return 0;}
Power representation of Xidianoj 1024 2