#include <iostream> #include <string> #include <sstream> #include <stack> using namespace std; int main(){ int t, k; cin >> t; for(int i=0; i<t; i++){ cin >> k; stack<int> bits; stringstream ss; while(k > 0){ bits.push(k % 2); k /= 2; } int brackets = 0; bool lastPlus1 = false; ss << "1"; bits.pop(); while(!bits.empty()){ if(lastPlus1){ ss << ")"; brackets++; } if(bits.top() == 1){ ss << "*(1+1)+1"; lastPlus1 = true; }else{ ss << "*(1+1)"; lastPlus1 = false; } bits.pop(); } string s(brackets, '('); cout << s << ss.str() << endl; } 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 45 46 47 48 49 50 51 | #include <iostream> #include <string> #include <sstream> #include <stack> using namespace std; int main(){ int t, k; cin >> t; for(int i=0; i<t; i++){ cin >> k; stack<int> bits; stringstream ss; while(k > 0){ bits.push(k % 2); k /= 2; } int brackets = 0; bool lastPlus1 = false; ss << "1"; bits.pop(); while(!bits.empty()){ if(lastPlus1){ ss << ")"; brackets++; } if(bits.top() == 1){ ss << "*(1+1)+1"; lastPlus1 = true; }else{ ss << "*(1+1)"; lastPlus1 = false; } bits.pop(); } string s(brackets, '('); cout << s << ss.str() << endl; } return 0; } |