#include <cstdio> #include <iostream> #include <string> using namespace std; int t,k; string print(int x, bool &naw) { naw = false; if(x == 0){ return string(); } if(x == 1){ return string{"1"}; } if(x == 2) { naw = true; return string{"1+1"}; } string r; if(x&1) { r="1+"; naw = true; } bool naw2; int x2 = x/2; string r2 = print(x2,naw2); if(x2>1) { if(naw2) return r+"(1+1)*("+r2+")"; else return r+"(1+1)*"+r2; } else { naw = true; return r+"1+1"; } } int main() { cin >> t; while(t--) { cin >> k; bool ile; cout << print(k, ile) << "\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <cstdio> #include <iostream> #include <string> using namespace std; int t,k; string print(int x, bool &naw) { naw = false; if(x == 0){ return string(); } if(x == 1){ return string{"1"}; } if(x == 2) { naw = true; return string{"1+1"}; } string r; if(x&1) { r="1+"; naw = true; } bool naw2; int x2 = x/2; string r2 = print(x2,naw2); if(x2>1) { if(naw2) return r+"(1+1)*("+r2+")"; else return r+"(1+1)*"+r2; } else { naw = true; return r+"1+1"; } } int main() { cin >> t; while(t--) { cin >> k; bool ile; cout << print(k, ile) << "\n"; } } |