#include <iostream> using namespace std; int t, n; /* Writes a number using only "1" (and operators "+", "*" as well) as an arithmetic formula */ void write_number(int n) { if (n == 1) cout << "1"; else if (n == 2) cout << "1+1"; else if (n % 2 == 0) { cout << "(1+1)*("; write_number(n / 2); cout << ")"; } else { cout << "1+("; write_number(n - 1); cout << ")"; } } int main() { cin >> t; for (int i = 0; i < t; ++i) { cin >> n; write_number(n); cout << endl; } }
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 | #include <iostream> using namespace std; int t, n; /* Writes a number using only "1" (and operators "+", "*" as well) as an arithmetic formula */ void write_number(int n) { if (n == 1) cout << "1"; else if (n == 2) cout << "1+1"; else if (n % 2 == 0) { cout << "(1+1)*("; write_number(n / 2); cout << ")"; } else { cout << "1+("; write_number(n - 1); cout << ")"; } } int main() { cin >> t; for (int i = 0; i < t; ++i) { cin >> n; write_number(n); cout << endl; } } |