#include <iostream> using namespace std; void print(bool * ar) { for (size_t i=0; i<30; ++i) cout << ar[i]; cout << endl; } void print(unsigned int pow) { if (pow==0) cout << "1"; else { for (size_t i=0; i<pow; ++i) { cout << "(1+1)"; if (i<pow-1) cout << "*"; } } } void calculate(unsigned int a) { bool bin[30]={false}; unsigned int j=0; while (a>0) { // bin[j] = a!= 2*(a/2); bin[j] = a&1; a = a/2; ++j; } int brackets = 0; int power = 0; bool started = false; bool needPlus = false; for (size_t i=0; i<30; ++i) { if (!bin[i]) { ++power; } else { if (i==0) { cout << "1"; needPlus = true; } else { if (started) { cout << "*(1+"; print(power); ++brackets; } else { if (needPlus) { cout << "+"; needPlus = false; } print(power); started = true; } } power = 1; } } for (size_t i=0; i< brackets; ++i) cout << ")"; cout << endl; } unsigned int a[100]; int main() { unsigned int t; cin >> t; for (size_t i=0; i<t; ++i) cin >> a[i]; for (size_t i=0; i<t; ++i) calculate(a[i]); 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 | #include <iostream> using namespace std; void print(bool * ar) { for (size_t i=0; i<30; ++i) cout << ar[i]; cout << endl; } void print(unsigned int pow) { if (pow==0) cout << "1"; else { for (size_t i=0; i<pow; ++i) { cout << "(1+1)"; if (i<pow-1) cout << "*"; } } } void calculate(unsigned int a) { bool bin[30]={false}; unsigned int j=0; while (a>0) { // bin[j] = a!= 2*(a/2); bin[j] = a&1; a = a/2; ++j; } int brackets = 0; int power = 0; bool started = false; bool needPlus = false; for (size_t i=0; i<30; ++i) { if (!bin[i]) { ++power; } else { if (i==0) { cout << "1"; needPlus = true; } else { if (started) { cout << "*(1+"; print(power); ++brackets; } else { if (needPlus) { cout << "+"; needPlus = false; } print(power); started = true; } } power = 1; } } for (size_t i=0; i< brackets; ++i) cout << ")"; cout << endl; } unsigned int a[100]; int main() { unsigned int t; cin >> t; for (size_t i=0; i<t; ++i) cin >> a[i]; for (size_t i=0; i<t; ++i) calculate(a[i]); return 0; } |