#include <iostream> #include <vector> #include <bitset> using namespace std; int main() { std::ios_base::sync_with_stdio (false); // cin.tie(NULL); int n, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; string binary = bitset<32>(t).to_string(); // cout << "t: " << t << endl; // cout << "binary: " << binary; vector<string> ones; int ones_spent = 0; // GENERATE for (int j = binary.length() - 1; j >= 0; j--) { int k = binary.length() - j - 1; // cout << "check j " << j << " k " << k << endl; if (binary[j] == '1') { string s; if (k == 0) { ones_spent += 1; s = "1"; } else { ones_spent += 2*k; s = "(1+1)"; for (int l = 1; l < k; l++) { s += "*(1+1)"; } } ones.push_back(s); } } // EMIT if ((ones_spent > 100) || (ones.size() == 0)) { cout << "NIE" << endl; } else { cout << ones[0]; for (int l = 1; l < ones.size(); l++) { cout << "+" << ones[l]; } cout << endl; } } cout << 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 52 53 54 55 56 57 58 59 | #include <iostream> #include <vector> #include <bitset> using namespace std; int main() { std::ios_base::sync_with_stdio (false); // cin.tie(NULL); int n, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; string binary = bitset<32>(t).to_string(); // cout << "t: " << t << endl; // cout << "binary: " << binary; vector<string> ones; int ones_spent = 0; // GENERATE for (int j = binary.length() - 1; j >= 0; j--) { int k = binary.length() - j - 1; // cout << "check j " << j << " k " << k << endl; if (binary[j] == '1') { string s; if (k == 0) { ones_spent += 1; s = "1"; } else { ones_spent += 2*k; s = "(1+1)"; for (int l = 1; l < k; l++) { s += "*(1+1)"; } } ones.push_back(s); } } // EMIT if ((ones_spent > 100) || (ones.size() == 0)) { cout << "NIE" << endl; } else { cout << ones[0]; for (int l = 1; l < ones.size(); l++) { cout << "+" << ones[l]; } cout << endl; } } cout << endl; return 0; } |