#include <iostream> #include <algorithm> #include <vector> using namespace std; int t, n; string answer(int n) { if (n == 1) return "1"; if (n % 2 == 1) { string result = "(1+" + answer(n-1) + ")"; return result; } if (n % 2 == 0) { string result = "((1+1)*" + answer(n/2) + ")"; return result; } return ""; } int main() { cin >> t; for (int i=0; i<t; i++) { cin >> n; cout << answer(n) << 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int t, n; string answer(int n) { if (n == 1) return "1"; if (n % 2 == 1) { string result = "(1+" + answer(n-1) + ")"; return result; } if (n % 2 == 0) { string result = "((1+1)*" + answer(n/2) + ")"; return result; } return ""; } int main() { cin >> t; for (int i=0; i<t; i++) { cin >> n; cout << answer(n) << endl; } return 0; } |