https://www.luogu.com.cn/problem/P1010
When just see this problem, a little ignorant, if this is a math problem
7 for example, you should first obtain 7 = 4 + 2 + 1;
I.e., there should be a first exploded largest number of 2, and then down recursive
- N is calculated how many times the power of 2 closest to the given;
- Minus 2 to the power of n with the number of the original, if this number is greater than 2, continue to search for new n;
- If the power is greater than 2, to perform the search power;
- Once the number of inputs to the function 0 (exit) or 1 (0-th power of 2) or 2 (a power of two, which does not require a time out), an output;
#include <bits/stdc++.h> using namespace std; int n; void search(int x){ if(!x) return; int p = 1,q = 0; cout << 2; while(x >= p){ p *= 2; q++; } q--; if(!q || q == 2) cout << "(" << q << ")"; if(q >= 3){ cout << "("; search(q); cout << ")"; } x -= p / 2; if(x){ cout << "+"; search(x); } } int main(){ ios::sync_with_stdio(0); cin >> n; search(n); return 0; }