Description
Any positive integer can be expressed by the power of 2. Example: {40%}
137 = 27 + 23 + 20
At the same time, the conventions are represented by parentheses, that is, AB can be expressed as a (B ).
137 can be expressed:
2 (7) + 2 (3) + 2 (0)
Further: 7 = 22 + 2 + 20 (expressed as 2 in 21)
3 = 2 + 20
So the last 137 can be expressed:
2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)
Another example:
1315 = 210 + 28 + 25 + 2 + 1
So 1315 can be expressed:
2 (2 (2 + 2 (0) + 2) + 2 (2 (2 + 2 (0) + 2 (2 (2) + 2 (0 )) + 2 + 2 (0)
Input Format
Input: positive integer (n ≤ 20000)
Output Format
Output: equal to 0, 2 of N (no space is allowed in the representation)
# Include <iostream> # include <cstdio> # include <cmath> using namespace STD; void solve (int n) {int I, B [50], P = 0; for (I = 0; n> 0; I ++, N> = 1) // the fastest way to convert binary data. B [I] = N & 1; for (I --; I> 1; I --) {If (B [I] & 1) {If (P) printf ("+"); printf ("2 ("); solve (I); printf (")"); P = 1 ;}} if (B [1] & 1) // Special Case 1 {If (p) printf ("+"); printf ("2"); P = 1 ;} if (B [0] & 1) // exception 2 {If (p) printf ("+"); printf ("2 (0 )");}} int main () {int N; while (scanf ("% d", & N )! = EOF) {solve (n); printf ("\ n") ;}return 0 ;}
Pid277/Integer splitting (recursion)