#include <iostream> using namespace std; int n,lastBit; bool firstBit; string makeAns(int i){ if(i==lastBit) return "(1+1)"; int bit=(n>>i)&1; if(bit){ if(firstBit){ firstBit=false; return "1+" + makeAns(i+1); }else return "(1+1)*(1+" + makeAns(i+1) + ")"; }else{ if(firstBit){ firstBit=false; return makeAns(i+1); }else return "(1+1)*" + makeAns(i+1); } } void test(){ cin>>n; firstBit=true; lastBit=-1; for(int a=0;a<31;++a){ if((n>>a)&1)lastBit=a; } if(n==1) cout<<1; else cout<<makeAns(0); cout<<"\n"; } int main(){ ios_base::sync_with_stdio(0); int t; cin>>t; for(int a=0;a<t;++a) test(); return 0; }
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 | #include <iostream> using namespace std; int n,lastBit; bool firstBit; string makeAns(int i){ if(i==lastBit) return "(1+1)"; int bit=(n>>i)&1; if(bit){ if(firstBit){ firstBit=false; return "1+" + makeAns(i+1); }else return "(1+1)*(1+" + makeAns(i+1) + ")"; }else{ if(firstBit){ firstBit=false; return makeAns(i+1); }else return "(1+1)*" + makeAns(i+1); } } void test(){ cin>>n; firstBit=true; lastBit=-1; for(int a=0;a<31;++a){ if((n>>a)&1)lastBit=a; } if(n==1) cout<<1; else cout<<makeAns(0); cout<<"\n"; } int main(){ ios_base::sync_with_stdio(0); int t; cin>>t; for(int a=0;a<t;++a) test(); return 0; } |