#include <iostream> using namespace std; string toBin(int n){ if(n == 0){ return "0"; } if(n == 1){ return "1"; } int i = (int)'0' + n%2; return toBin(n/2) + (char)i; } int countOnes(string bin){ int sum = 0; for(int x=0; x<bin.size(); x++){ if(bin[x] == '1'){ sum++; } } return sum; } int main(){ int t; cin >> t; while(t--){ unsigned int n; cin >> n; if(n == 1){ cout << "1" << endl; } else { string bin = toBin(n); for(int x=0; x<countOnes(bin)-1; x++) { cout << "("; } cout << "(1+1)"; for(int x=1; x<bin.size(); x++){ if(bin[x] == '1'){ cout << "+1)"; } if(x != bin.size() - 1){ cout << "*(1+1)"; } } 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 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 | #include <iostream> using namespace std; string toBin(int n){ if(n == 0){ return "0"; } if(n == 1){ return "1"; } int i = (int)'0' + n%2; return toBin(n/2) + (char)i; } int countOnes(string bin){ int sum = 0; for(int x=0; x<bin.size(); x++){ if(bin[x] == '1'){ sum++; } } return sum; } int main(){ int t; cin >> t; while(t--){ unsigned int n; cin >> n; if(n == 1){ cout << "1" << endl; } else { string bin = toBin(n); for(int x=0; x<countOnes(bin)-1; x++) { cout << "("; } cout << "(1+1)"; for(int x=1; x<bin.size(); x++){ if(bin[x] == '1'){ cout << "+1)"; } if(x != bin.size() - 1){ cout << "*(1+1)"; } } cout << endl; } } } |