Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- using namespace std;
- class State {
- public:
- unsigned int current;
- char lastLoop;
- list<char> prevLoops; //0: ending in +7. 1: ending in /2. 2: ending in *3. 3: ending in -5.
- };
- int main() {
- State cur;
- cur.current=2018;
- cur.lastLoop=0;
- list<State> nodes;
- State tmp;
- size_t counter=0;
- for (;;) {
- if (cur.lastLoop!=0) if (cur.current&1) {
- tmp.current=(cur.current+7)>>1;
- tmp.prevLoops=cur.prevLoops;
- tmp.prevLoops.push_back(1);
- if (tmp.current==2017) {
- cur=tmp;
- break;
- }
- tmp.lastLoop=1;
- nodes.push_back(tmp);
- }
- if (cur.lastLoop!=1) if (!(cur.current&1)) {
- tmp.current=(cur.current>>1)+7;
- tmp.prevLoops=cur.prevLoops;
- tmp.prevLoops.push_back(0);
- if (tmp.current==2017) {
- cur=tmp;
- break;
- }
- tmp.lastLoop=0;
- nodes.push_back(tmp);
- }
- if (cur.lastLoop!=2) {
- tmp.current=(cur.current+(cur.current<<1)-5);
- tmp.prevLoops=cur.prevLoops;
- tmp.prevLoops.push_back(3);
- tmp.lastLoop=3;
- nodes.push_back(tmp);
- }
- if (cur.lastLoop!=3) {
- tmp.current=cur.current-5;
- tmp.current+=tmp.current<<1;
- tmp.prevLoops=cur.prevLoops;
- tmp.prevLoops.push_back(2);
- if (tmp.current==2017) {
- cur=tmp;
- break;
- }
- tmp.lastLoop=2;
- nodes.push_back(tmp);
- }
- ++counter;
- cur=nodes.front();
- nodes.pop_front();
- }
- unsigned int n1=2018,n2;
- printf("2011+7=2018.\n");
- list<char>::const_iterator e=cur.prevLoops.end();
- for (list<char>::const_iterator it=cur.prevLoops.begin();it!=e;++it) {
- switch (*it) {
- case 0:
- n2=n1>>1;
- printf("%u/2=%u.\n",n1,n2);
- n1=n2;
- n2=n1+7;
- printf("%u+7=%u.\n",n1,n2);
- break;
- case 1:
- n2=n1+7;
- printf("%u+7=%u.\n",n1,n2);
- n1=n2;
- n2=n1>>1;
- printf("%u/2=%u.\n",n1,n2);
- break;
- case 2:
- n2=n1-5;
- printf("%u-5=%u.\n",n1,n2);
- n1=n2;
- n2=n1+(n1<<1);
- printf("%u*3=%u.\n",n1,n2);
- break;
- case 3:
- n2=n1+(n1<<1);
- printf("%u*3=%u.\n",n1,n2);
- n1=n2;
- n2=n1-5;
- printf("%u-5=%u.\n",n1,n2);
- break;
- }
- n1=n2;
- }
- printf("2017-5=2012.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement