Advertisement
Guest User

Untitled

a guest
Oct 13th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <memory.h>
  5. #include "math.h"
  6.  
  7. using namespace std;
  8.  
  9. #define MAXNUM 3
  10.  
  11. static vector<int*> permutations;
  12.  
  13. struct Data
  14. {
  15.     int* arrangement;
  16.     bool* isEmpty;
  17.     int nextNumberToInsert;
  18. };
  19.  
  20. void Permute(struct Data *input)
  21. {
  22.     if (input->nextNumberToInsert == MAXNUM)
  23.     {
  24.         for (int i=0; i<=MAXNUM; i++)
  25.         {
  26.             if (input->isEmpty[i])
  27.             {
  28.                 input->arrangement[i] = input->nextNumberToInsert;
  29.                 break;
  30.             }
  31.         }
  32.  
  33.         cout << "Pushing onto back: ";
  34.         for (int a=0; a<=MAXNUM; a++) cout << input->arrangement[a] << " ";
  35.         permutations.push_back(input->arrangement);
  36.         cout << endl;
  37.         cout << "Back is: ";
  38.         for (int a=0; a<=MAXNUM; a++) cout << permutations.back()[a] << " ";
  39.         cout << endl;
  40.         cout << "Front is: ";
  41.         for (int a=0; a<=MAXNUM; a++) cout << permutations.front()[a] << " ";
  42.         cout << endl;
  43.         sleep(5);
  44.     }
  45.     else
  46.     {
  47.         // Iterate over all the empty spaces, inserting the next number in each, spawning a new search branch each time.
  48.         for (int i=0; i<=MAXNUM; i++)
  49.         {
  50.             if (input->isEmpty[i])
  51.             {
  52.                 // Copy the input data
  53.                 struct Data *inputCopy = new struct Data;
  54.                 inputCopy->arrangement = new int[MAXNUM+1];
  55.                 inputCopy->isEmpty = new bool[MAXNUM+1];
  56.                 memcpy(inputCopy->arrangement, input->arrangement, (MAXNUM+1)*sizeof(int));
  57.                 memcpy(inputCopy->isEmpty, input->isEmpty, (MAXNUM+1)*sizeof(bool));
  58.  
  59.                 inputCopy->arrangement[i] = input->nextNumberToInsert;
  60.                 inputCopy->isEmpty[i] = false;
  61.                 inputCopy->nextNumberToInsert = input->nextNumberToInsert + 1;
  62.  
  63.                 Permute(inputCopy);
  64.  
  65.                 delete inputCopy->arrangement;
  66.                 delete inputCopy->isEmpty;
  67.                 delete inputCopy;
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     struct Data seed;
  76.     seed.arrangement = new int[MAXNUM+1];
  77.     seed.isEmpty = new bool[MAXNUM+1];
  78.  
  79.     seed.nextNumberToInsert = 0;
  80.     memset(seed.isEmpty, true, (MAXNUM+1)*sizeof(bool));
  81.  
  82.     Permute(&seed);
  83.  
  84.     cout << permutations[0][0] << endl;
  85.  
  86.     cout << "permutations complete: " << permutations.size() << endl;
  87.  
  88.     return 0;
  89.  
  90.     std::set<int> pandigital_product;
  91.     //for (std::vector<int*>::iterator iter=permutations.begin(); iter != permutations.end(); iter++)
  92.     for (int index=0; index<permutations.size(); index++)
  93.     {
  94.         cout << index << endl;
  95.         for (int a=0; a<=MAXNUM; a++) cout << (permutations[index])[a] << endl;
  96.         cout << endl;
  97.         sleep(1);
  98.         for (int i=0; i<=MAXNUM-2; i++)
  99.         {
  100.             for (int j=i+1; j<=MAXNUM-1; j++)
  101.             {
  102.                 long lhs1 = 0;
  103.                 for (int a=0; a<=i; a++)
  104.                 {
  105.                     lhs1 *= 10;
  106.                     lhs1 += (permutations[index])[a];
  107.                 }
  108.  
  109.                 long lhs2 = 0;
  110.                 for (int a=i+1; a<=j; a++)
  111.                 {
  112.                     lhs2 *= 10;
  113.                     lhs2 += (permutations[index])[a];
  114.                 }
  115.  
  116.                 long rhs = 0;
  117.                 for (int a=j+1; a<=MAXNUM; a++)
  118.                 {
  119.                     rhs *= 10;
  120.                     rhs += (permutations[index])[a];
  121.                 }
  122.  
  123. //                for (int a=0; a<=9; a++) cout << (*iter)[a];
  124. //                cout << endl;
  125. //                cout << lhs1 << ", " << lhs2 << ", " << rhs << endl;
  126. //                sleep(1);
  127.  
  128.                 if (lhs1*lhs2 == rhs) pandigital_product.insert(rhs);
  129.             }
  130.         }
  131.     }
  132.  
  133.     int sum = 0;
  134.     for (std::set<int>::const_iterator iter=pandigital_product.begin(); iter != pandigital_product.end(); iter++)
  135.     {
  136.         sum += *iter;
  137.     }
  138.     cout << sum;
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement