#include <iostream> #include <string> std::string turnIntoOnes(int n) { std::string s = ""; if (n <= 5) { s += "1"; for (int i = 1; i < n; i++) s += "+1"; return s; } if (n%5 == 0) { s += "("; s += turnIntoOnes(n/5); s += ")*(1+1+1+1+1)"; return s; } if (n%3 == 0) { s += "("; s += turnIntoOnes(n/3); s += ")*(1+1+1)"; return s; } std::string b = turnIntoOnes(n/2); s += "("; s += b; s += ")*(1+1)"; if (n%2!=0) s+="+1"; return s; } void testcase() { int k; std::cin >> k; std::string s = turnIntoOnes(k); std::cout << s; } int main() { std::ios_base::sync_with_stdio(0); int t; std::cin >> t; for (int i = 0; i < t; i++) { testcase(); std::cout << std::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 | #include <iostream> #include <string> std::string turnIntoOnes(int n) { std::string s = ""; if (n <= 5) { s += "1"; for (int i = 1; i < n; i++) s += "+1"; return s; } if (n%5 == 0) { s += "("; s += turnIntoOnes(n/5); s += ")*(1+1+1+1+1)"; return s; } if (n%3 == 0) { s += "("; s += turnIntoOnes(n/3); s += ")*(1+1+1)"; return s; } std::string b = turnIntoOnes(n/2); s += "("; s += b; s += ")*(1+1)"; if (n%2!=0) s+="+1"; return s; } void testcase() { int k; std::cin >> k; std::string s = turnIntoOnes(k); std::cout << s; } int main() { std::ios_base::sync_with_stdio(0); int t; std::cin >> t; for (int i = 0; i < t; i++) { testcase(); std::cout << std::endl; } return 0; } |