#include <iostream> #include <vector> using namespace std; bool solve (); string two = "(1+1)"; string one = "+1)"; bool solve () { vector<int> vec; int n; cin >> n; int count_ones = 0; if(n == 1) { cout <<"1"; cout << endl; return true; } while (n > 1) { if(n % 2 == 1) { vec.push_back (1); n --; count_ones ++; } else { vec.push_back (2); n/=2; } } for(int i = 0; i < count_ones; ++i) { cout << "("; } for(int i = vec.size () - 1; i >= 0; --i) { if(vec[i] == 2) { cout << two; } else { cout << one; } if(i > 0 && vec [i-1] == 2) { cout << "*"; } } cout << endl; return true; } int main () { ios_base::sync_with_stdio (false); int t; cin >> t; for (int i = 0; i < t; ++i) { solve (); } }
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 52 53 54 | #include <iostream> #include <vector> using namespace std; bool solve (); string two = "(1+1)"; string one = "+1)"; bool solve () { vector<int> vec; int n; cin >> n; int count_ones = 0; if(n == 1) { cout <<"1"; cout << endl; return true; } while (n > 1) { if(n % 2 == 1) { vec.push_back (1); n --; count_ones ++; } else { vec.push_back (2); n/=2; } } for(int i = 0; i < count_ones; ++i) { cout << "("; } for(int i = vec.size () - 1; i >= 0; --i) { if(vec[i] == 2) { cout << two; } else { cout << one; } if(i > 0 && vec [i-1] == 2) { cout << "*"; } } cout << endl; return true; } int main () { ios_base::sync_with_stdio (false); int t; cin >> t; for (int i = 0; i < t; ++i) { solve (); } } |