Project Euler Homework

024.cpp

Go to the documentation of this file.
00001 
00010 #include <iostream>
00011 using namespace std;
00012 
00013 const int digits = 10;
00038 class permutation
00039 {
00041   static unsigned count;
00043   static bool flag;
00045   unsigned short x[digits];
00047   int begin;
00049   int end;
00050 public:
00051     permutation ();
00052     permutation (unsigned short *y, int a, int b);
00054   void swap (int a, int b);
00056   void printP ();
00058   void move_to (unsigned k);
00059 };
00060 
00061 unsigned
00062   permutation::count = 0;
00063 bool permutation::flag = 0;
00064 
00065 permutation::permutation ()
00066 {
00067   unsigned short *p = x;
00068   for (int i = 0; i < digits; i++, p++)
00069     *p = i;
00070 
00071   begin = 0;
00072   end = digits - 1;
00073 }
00074 
00075 permutation::permutation (unsigned short *y, int a, int b)
00076 {
00077   unsigned short *p = x;
00078   for (int i = 0; i < digits; i++, p++, y++)
00079     *p = *y;
00080 
00081   begin = a;
00082   end = b;
00083 }
00084 
00085 void
00086 permutation::printP ()
00087 {
00088   for (int i = 0; i < digits; i++)
00089     cout << x[i];
00090   cout << endl;
00091 }
00092 
00093 void
00094 permutation::swap (int a, int b)
00095 {
00096   unsigned short tmp = x[a];
00097   x[a] = x[b];
00098   x[b] = tmp;
00099 }
00100 
00101 void
00102 permutation::move_to (unsigned k)
00103 {
00104   if (end - begin == 1)         //only two digits
00105     {
00106       count += 2;               //only two possible permutations
00107       if (count == k)           //reached desired permutation
00108         {
00109           this->swap (begin, end);
00110           this->printP ();      //print the permutation
00111           flag = 1;             //stop the recursion
00112           return;
00113         }
00114       else if (count == k + 1)  //or go beyond the desired permutation
00115         {
00116           this->printP ();
00117           flag = 1;
00118           return;
00119         }
00120       else if (count > k)       //error, stop the process
00121         {
00122           cout << "An error had encountered when processing digits " << begin
00123             << " to " << end << ":\n";
00124           this->printP ();
00125           cout << "Count = " << count << endl;
00126           flag = 1;
00127           return;
00128         }
00129     }
00130 
00131   else                          //if more than two digits
00132     for (int idx = begin; idx <= end; idx++)
00133       {
00134         this->swap (begin, idx);        //try new permutation
00135 
00136         permutation *newlist = new permutation (x, begin + 1, end);     //copy to new string
00137         newlist->move_to (k);   //recursion relies on here
00138         delete newlist;
00139 
00140         if (flag)               //break the loop if the desired value has been reached
00141           break;
00142       }
00143 }
00144 
00145 int
00146 main ()
00147 {
00148   permutation list;
00149   list.move_to (1000000);       //find the one millionth permutation
00150 
00151   return 0;
00152 }
00153 
 All Classes Files Functions Variables