#include <iostream> #include <sstream> enum LastAction {NOT_SET, ADD, MULT}; class Jed { private: const unsigned int original_val; unsigned int val; int num_ones; std::stringstream equasion; LastAction lastAction; unsigned int numOfBrackets; void appendIncrement() { this->num_ones --; this->val --; this->equasion << "(1"; this->lastAction = ADD; this->numOfBrackets++; } void append2factor() { this->num_ones -= 2; this->val = val/2; this->equasion << "(1+1)"; this->lastAction = MULT; } void updateWithLastAction() { if(this->lastAction == MULT) { this->equasion << "*"; } else if(this->lastAction == ADD) { this->equasion << "+"; } } void appendMissingBrackets() { for(int i = this->numOfBrackets; i > 0; i--) { this->equasion << ")"; } } public: Jed(unsigned int orig, int max_ones=100):original_val(orig), val(orig), lastAction(NOT_SET), numOfBrackets(0) { num_ones = max_ones; }; virtual ~Jed() {} void processStep() { updateWithLastAction(); if(val & 0x01) { appendIncrement(); } else { append2factor(); } } const char* getCurrentEquasion() { return equasion.str().c_str(); } void processSteps() { while((this->val > 0) && (this->num_ones > 0)) { this->processStep(); } if(this->val > 1) { equasion.str(""); equasion << "NIE"; } else { appendMissingBrackets(); } equasion << std::endl; } }; void notnecessary() { Jed j(100); j.processStep(); j.getCurrentEquasion(); j.processSteps(); } unsigned int parseSingleCase(std::istream &is) { unsigned int result = 0; is >> result; return result; } void runJed(std::istream &is, std::ostream &os) { int num = 0; is >> num; for(int i = 0; i < num; i++) { unsigned int val = parseSingleCase(is); Jed j(val); j.processSteps(); os << j.getCurrentEquasion(); } } int main(int argc, char* argv[]) { runJed(std::cin, std::cout); }
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 | #include <iostream> #include <sstream> enum LastAction {NOT_SET, ADD, MULT}; class Jed { private: const unsigned int original_val; unsigned int val; int num_ones; std::stringstream equasion; LastAction lastAction; unsigned int numOfBrackets; void appendIncrement() { this->num_ones --; this->val --; this->equasion << "(1"; this->lastAction = ADD; this->numOfBrackets++; } void append2factor() { this->num_ones -= 2; this->val = val/2; this->equasion << "(1+1)"; this->lastAction = MULT; } void updateWithLastAction() { if(this->lastAction == MULT) { this->equasion << "*"; } else if(this->lastAction == ADD) { this->equasion << "+"; } } void appendMissingBrackets() { for(int i = this->numOfBrackets; i > 0; i--) { this->equasion << ")"; } } public: Jed(unsigned int orig, int max_ones=100):original_val(orig), val(orig), lastAction(NOT_SET), numOfBrackets(0) { num_ones = max_ones; }; virtual ~Jed() {} void processStep() { updateWithLastAction(); if(val & 0x01) { appendIncrement(); } else { append2factor(); } } const char* getCurrentEquasion() { return equasion.str().c_str(); } void processSteps() { while((this->val > 0) && (this->num_ones > 0)) { this->processStep(); } if(this->val > 1) { equasion.str(""); equasion << "NIE"; } else { appendMissingBrackets(); } equasion << std::endl; } }; void notnecessary() { Jed j(100); j.processStep(); j.getCurrentEquasion(); j.processSteps(); } unsigned int parseSingleCase(std::istream &is) { unsigned int result = 0; is >> result; return result; } void runJed(std::istream &is, std::ostream &os) { int num = 0; is >> num; for(int i = 0; i < num; i++) { unsigned int val = parseSingleCase(is); Jed j(val); j.processSteps(); os << j.getCurrentEquasion(); } } int main(int argc, char* argv[]) { runJed(std::cin, std::cout); } |