#include<iostream> #include<vector> #include<algorithm> #include<cstdlib> #include<queue> #include<cmath> #include<stack> #include <fstream> using namespace std; int R; vector<int> P; void print( char c) { switch( c) { case '(': P.push_back(-1); break; case ')': P.push_back(-2); break; case '*': P.push_back(-3); break; case '+': P.push_back(-4); break; } } void factorise(int p) { int r=0; if( p&1 ) { p--; r = 1; } print('('); while( !(p&1) ) { print('('); P.push_back(2); print(')'); print('*'); p/=2; } if(p > 1) factorise(p); else P.pop_back(); if(r == 1) { print('+'); P.push_back(1); } print(')'); } void printOnes(int c) { for(int i=0; i<c-1; i++) cout<<"1+"; cout<<1; } void printQ(int c) { if(c >= 0) printOnes(c); else switch(c) { case -1: cout<<"("; break; case -2: cout<<")"; break; case -3: cout<<"*"; break; case -4: cout<<"+"; break; } } void printS() { for(int i=0; i<P.size(); i++) { int c = P[i]; printQ(c); if(c>=0) R+=c; } /*stack<int> Q; while(!P.empty()) { int c = P.top(); Q.push_back(c); //printQ(c); P.pop(); } //cout<<endl; while(!Q.empty()) { int c = Q.top(); printQ(c); if(c>=0) R+=c; Q.pop(); }*/ cout<<endl; P.clear(); } int main() { ios_base::sync_with_stdio(0); int t; cin>>t; for(int TT=0; TT<t; TT++) { int k; cin>>k; R = 0; if(k <= 3) { printOnes(k); cout<<endl; } else { factorise(k); printS(); }//cout<<R<<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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include<iostream> #include<vector> #include<algorithm> #include<cstdlib> #include<queue> #include<cmath> #include<stack> #include <fstream> using namespace std; int R; vector<int> P; void print( char c) { switch( c) { case '(': P.push_back(-1); break; case ')': P.push_back(-2); break; case '*': P.push_back(-3); break; case '+': P.push_back(-4); break; } } void factorise(int p) { int r=0; if( p&1 ) { p--; r = 1; } print('('); while( !(p&1) ) { print('('); P.push_back(2); print(')'); print('*'); p/=2; } if(p > 1) factorise(p); else P.pop_back(); if(r == 1) { print('+'); P.push_back(1); } print(')'); } void printOnes(int c) { for(int i=0; i<c-1; i++) cout<<"1+"; cout<<1; } void printQ(int c) { if(c >= 0) printOnes(c); else switch(c) { case -1: cout<<"("; break; case -2: cout<<")"; break; case -3: cout<<"*"; break; case -4: cout<<"+"; break; } } void printS() { for(int i=0; i<P.size(); i++) { int c = P[i]; printQ(c); if(c>=0) R+=c; } /*stack<int> Q; while(!P.empty()) { int c = P.top(); Q.push_back(c); //printQ(c); P.pop(); } //cout<<endl; while(!Q.empty()) { int c = Q.top(); printQ(c); if(c>=0) R+=c; Q.pop(); }*/ cout<<endl; P.clear(); } int main() { ios_base::sync_with_stdio(0); int t; cin>>t; for(int TT=0; TT<t; TT++) { int k; cin>>k; R = 0; if(k <= 3) { printOnes(k); cout<<endl; } else { factorise(k); printS(); }//cout<<R<<endl; } return 0; } |