Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. void printElementsCntPerCycle(int *permutation, int *universal, int size) {
  2.     int elementsPerIndependentCycle[size]{0};
  3.     int currentIndependentCycle = 0;
  4.  
  5.     int cyclesCnt = 0;
  6.  
  7.     int passedIndexes[size]{0};
  8.  
  9.     int wantedIndex = 0;
  10.     int wanted = universal[wantedIndex];
  11.  
  12.     int operatingIndex = 0;
  13.     int upElement = universal[operatingIndex];
  14.     int downElement = permutation[operatingIndex];
  15.  
  16.     while (cyclesCnt < size) {
  17.         elementsPerIndependentCycle[currentIndependentCycle]++;
  18.         while (downElement != wanted) {
  19.             elementsPerIndependentCycle[currentIndependentCycle]++;
  20.             passedIndexes[operatingIndex] = -1;
  21.             operatingIndex = getIndex(universal, size, downElement);
  22.             upElement = universal[operatingIndex];
  23.             downElement = permutation[operatingIndex];
  24.             cyclesCnt++;
  25.         }
  26.         currentIndependentCycle++;
  27.         passedIndexes[operatingIndex] = -1;
  28.         operatingIndex = getFirstFreeIndex(passedIndexes, size);
  29.         if (operatingIndex == -1) {
  30.             break;
  31.         }
  32.         wanted = universal[operatingIndex];
  33.         upElement = universal[operatingIndex];
  34.         downElement = permutation[operatingIndex];
  35.     }
  36.  
  37.     for(int i = 0; i < currentIndependentCycle; i++){
  38.         cout << "Cycle " << i + 1 << " has " << elementsPerIndependentCycle[i] << " elements." << endl;
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement