#include<iostream> #include<string> #include<algorithm> using namespace std; string solve(int arg){ if(arg==1) return "1"; if(arg==2) return "1+1"; if(arg==3) return "1+1+1"; if(arg%3==0) return "(1+1+1)*("+solve(arg/3)+")"; if(arg%2) return "1+"+solve(arg-1); else return "(1+1)*("+solve(arg/2)+")"; } int main(int argc, char** argv){ int t, k; string solution; int num_ones; cin >> t; for(int casenum=0; casenum<t; casenum++){ cin >> k; solution = solve(k); num_ones = count( solution.begin(), solution.end(), '1'); if (num_ones > 100) cout << "NIE" << endl; else cout << solution << 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 | #include<iostream> #include<string> #include<algorithm> using namespace std; string solve(int arg){ if(arg==1) return "1"; if(arg==2) return "1+1"; if(arg==3) return "1+1+1"; if(arg%3==0) return "(1+1+1)*("+solve(arg/3)+")"; if(arg%2) return "1+"+solve(arg-1); else return "(1+1)*("+solve(arg/2)+")"; } int main(int argc, char** argv){ int t, k; string solution; int num_ones; cin >> t; for(int casenum=0; casenum<t; casenum++){ cin >> k; solution = solve(k); num_ones = count( solution.begin(), solution.end(), '1'); if (num_ones > 100) cout << "NIE" << endl; else cout << solution << endl; } return 0; } |