Power of 56-2, power of 56-2
Http://lx.lanqiao.cn/problem.page? The time limit for the power expression of gpid = T235 algorithm training 2: 1.0 s memory limit: 512.0 MBProblem description any positive integer can be expressed in binary notation. For example, the binary notation of 137 is 10001001.
This binary representation is written as the sum of the power of the second, so that the power of the second is ranked first. The following expression can be obtained: 137 = 2 ^ 7 + 2 ^ 3 + 2 ^ 0
The power is represented by parentheses, that is, a ^ B is represented as a (B)
In this case, 137 can be expressed as: 2 (7) + 2 (3) + 2 (0)
Further: 7 = 2 ^ 2 + 2 + 2 ^ 0 (2 ^ 1 is expressed as 2)
3 = 2 + 2 ^ 0
So the last 137 can be expressed as: 2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0)
Another example: 1315 = 2 ^ 10 + 2 ^ 8 + 2 ^ 5 + 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) positive integer in the input format (1 <= n <= 20000) the output format conforms to the agreed 0, 2 Representation of n (the representation cannot contain spaces) example input 137 sample output 2 (2 (2) + 2 + 2 (0) + 2 (2 + 2 (0) + 2 (0) sample input 1315 sample output 2 (2 (2 + 2 (0) + 2) + 2 (2 + 2 (0 ))) + 2 (2 (2) + 2 (0) + 2 + 2 (0) prompts that Recursive Implementation is relatively simple and can be output on one side of Recursion
# Include <iostream> # include <cstring> using namespace std; void print (int n) {int a [20000] = {0 }; int len = 0; while (n! = 0) {// obtain the binary a [len ++] = n % 2; n/= 2;} // for (int I = 0; I <len; I ++) // cout <a [I]; int flag = 1; for (int I = len-1; I> = 0; I --) {if (a [I] = 1) {if (flag) {cout <"2"; flag = 0 ;}else {cout <"+ 2 ";} if (I = 0) {cout <"(0)" ;}else if (I = 1) {;} else if (I> 1) {// continue decomposing cout for the power of> 1 <"("; print (I); cout <")" ;}}} int main () {int n; cin> n; print (n); return 0 ;}