#include <bits/stdc++.h>
using namespace std;
string s;
void f(int n){
s.clear();
if(n==1){
cout << "1" << endl;
return;
}
int tab[30];
int a,b=0,i=0;
while(n){
tab[i++]=n%2;
n/=2;
}
i--;
if(tab[i]==1) s+="(1+1)";
if(tab[i]==0) s+="(1+1)";
if(tab[i-1]==1) s+="+1";
i--;
while(i>0){
if(s.back()!=')') s+=')';
if(tab[i-1]==1) s+="*(1+1)+1";
if(tab[i-1]==0) s+="*(1+1)";
i--;
}
int ile1=0,ile2=0;
for(int i=0;i<s.size();i++)
if(s[i]=='(')
ile1++;
else if(s[i]==')')
ile2++;
for(int i=0;i<ile2-ile1;i++)
cout << '(';
cout << s << endl;
}
int main(){
ios_base::sync_with_stdio(0);
int n,t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
f(n);
}
}
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 | #include <bits/stdc++.h> using namespace std; string s; void f(int n){ s.clear(); if(n==1){ cout << "1" << endl; return; } int tab[30]; int a,b=0,i=0; while(n){ tab[i++]=n%2; n/=2; } i--; if(tab[i]==1) s+="(1+1)"; if(tab[i]==0) s+="(1+1)"; if(tab[i-1]==1) s+="+1"; i--; while(i>0){ if(s.back()!=')') s+=')'; if(tab[i-1]==1) s+="*(1+1)+1"; if(tab[i-1]==0) s+="*(1+1)"; i--; } int ile1=0,ile2=0; for(int i=0;i<s.size();i++) if(s[i]=='(') ile1++; else if(s[i]==')') ile2++; for(int i=0;i<ile2-ile1;i++) cout << '('; cout << s << endl; } int main(){ ios_base::sync_with_stdio(0); int n,t; scanf("%d",&t); while(t--){ scanf("%d",&n); f(n); } } |
English