Advertisement
Nbrevu

2011.cpp

Jan 2nd, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <list>
  2.  
  3. using namespace std;
  4.  
  5. class State {
  6. public:
  7.     unsigned int current;
  8.     char lastLoop;
  9.     list<char> prevLoops;   //0: ending in +7. 1: ending in /2. 2: ending in *3. 3: ending in -5.
  10. };
  11.  
  12. int main()  {
  13.     State cur;
  14.     cur.current=2018;
  15.     cur.lastLoop=0;
  16.     list<State> nodes;
  17.     State tmp;
  18.     size_t counter=0;
  19.     for (;;)    {
  20.         if (cur.lastLoop!=0) if (cur.current&1) {
  21.             tmp.current=(cur.current+7)>>1;
  22.             tmp.prevLoops=cur.prevLoops;
  23.             tmp.prevLoops.push_back(1);
  24.             if (tmp.current==2017)  {
  25.                 cur=tmp;
  26.                 break;
  27.             }
  28.             tmp.lastLoop=1;
  29.             nodes.push_back(tmp);
  30.         }
  31.         if (cur.lastLoop!=1) if (!(cur.current&1))  {
  32.             tmp.current=(cur.current>>1)+7;
  33.             tmp.prevLoops=cur.prevLoops;
  34.             tmp.prevLoops.push_back(0);
  35.             if (tmp.current==2017)  {
  36.                 cur=tmp;
  37.                 break;
  38.             }
  39.             tmp.lastLoop=0;
  40.             nodes.push_back(tmp);
  41.         }
  42.         if (cur.lastLoop!=2)    {
  43.             tmp.current=(cur.current+(cur.current<<1)-5);
  44.             tmp.prevLoops=cur.prevLoops;
  45.             tmp.prevLoops.push_back(3);
  46.             tmp.lastLoop=3;
  47.             nodes.push_back(tmp);
  48.         }
  49.         if (cur.lastLoop!=3)    {
  50.             tmp.current=cur.current-5;
  51.             tmp.current+=tmp.current<<1;
  52.             tmp.prevLoops=cur.prevLoops;
  53.             tmp.prevLoops.push_back(2);
  54.             if (tmp.current==2017)  {
  55.                 cur=tmp;
  56.                 break;
  57.             }
  58.             tmp.lastLoop=2;
  59.             nodes.push_back(tmp);
  60.         }
  61.         ++counter;
  62.         cur=nodes.front();
  63.         nodes.pop_front();
  64.     }
  65.     unsigned int n1=2018,n2;
  66.     printf("2011+7=2018.\n");
  67.     list<char>::const_iterator e=cur.prevLoops.end();
  68.     for (list<char>::const_iterator it=cur.prevLoops.begin();it!=e;++it)    {
  69.         switch (*it)    {
  70.             case 0:
  71.                 n2=n1>>1;
  72.                 printf("%u/2=%u.\n",n1,n2);
  73.                 n1=n2;
  74.                 n2=n1+7;
  75.                 printf("%u+7=%u.\n",n1,n2);
  76.                 break;
  77.             case 1:
  78.                 n2=n1+7;
  79.                 printf("%u+7=%u.\n",n1,n2);
  80.                 n1=n2;
  81.                 n2=n1>>1;
  82.                 printf("%u/2=%u.\n",n1,n2);
  83.                 break;
  84.             case 2:
  85.                 n2=n1-5;
  86.                 printf("%u-5=%u.\n",n1,n2);
  87.                 n1=n2;
  88.                 n2=n1+(n1<<1);
  89.                 printf("%u*3=%u.\n",n1,n2);
  90.                 break;
  91.             case 3:
  92.                 n2=n1+(n1<<1);
  93.                 printf("%u*3=%u.\n",n1,n2);
  94.                 n1=n2;
  95.                 n2=n1-5;
  96.                 printf("%u-5=%u.\n",n1,n2);
  97.                 break;
  98.         }
  99.         n1=n2;
  100.     }
  101.     printf("2017-5=2012.\n");
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement