// permutation.h : List all the permutation subsets of a certian set. // #pragma once #include <iostream> #include <iomanip> usingnamespace std; // Compute P(N, M). N is the total number, M is the selected number. template<int N, int M> class CPermutation { private: int** m_result; // two-dimension array of [m_nCount][M] int m_nCount; // how many results int m_nIndex; // 0 - m_nCount - 1 public: // List all possible results by nesting void Count() { m_nIndex =0; int result[N], used[N]; for (int i =0; i < N; i++) used[i] =0; CountRecur(result, used, 0); } void CountRecur(int result[M], int used[M], int i) { for (int k =0; k < N; k++) { if (used[k]) continue ; result[i] = k; used[k] =1; if (i < M -1) CountRecur(result, used, i +1); else Add(result); used[k] =0; } } // Save the result void Add(int sz[M]) { memcpy(m_result[m_nIndex], sz, M *sizeof(int)); ++m_nIndex; } // Count the number of subsets // C(N, M) = N! / ((N - M)! * M!) staticint NumberOfResult() { if (N <=0|| M <=0|| M > N) return0; int result =1; for (int i =0; i < M; i++) result *= N - i; return result; } // Print them to the standard output device void Print() { for (int i =0; i < m_nCount; i++) { cout << setw(3) << setfill('') << i +1<<":"; for (int j =0; j < M; j++) cout << setw(3) << setfill('') << m_result[i][j] +1; cout << endl; } } CPermutation() { // allocate memories for the result m_nCount = NumberOfResult(); m_result =newint*[m_nCount]; for (int i =0; i < m_nCount; i++) m_result[i] =newint[M]; } ~CPermutation() { // deallocate memories for the result for (int i =0; i < m_nCount; i++) delete[] m_result[i]; delete[] m_result; } };
// combination.h : list all the subsets of a certian set // #pragma once #include <iostream> #include <iomanip> usingnamespace std; // List all the M-subsets of the N-set template<int N, int M> class CCombination { private: int** m_result; // two-dimension array of m_nCount * M int m_nCount; // how many results of M-length array public: CCombination() { // allocate memories for the result m_nCount = NumberOfResult(); m_result =newint*[m_nCount]; for (int i =0; i < m_nCount; i++) m_result[i] =newint[M]; } ~CCombination() { // deallocate memories for the result for (int i =0; i < m_nCount; i++) delete[] m_result[i]; delete[] m_result; } // process of counting void Count() { int sz[M]; int nResultCount =0; CountRecur(sz, 0, 0, nResultCount); } // Print them to the standard output device void Print() { using std::cout; using std::setw; using std::setfill; using std::endl; for (int i =0; i < m_nCount; i++) { cout << setw(3) << setfill('') << i +1<<":"; for (int j =0; j < M; j++) cout << setw(3) << setfill('') << m_result[i][j] +1; cout << endl; } } private: // Count the number of subsets // C(N, M) = N! / ((N - M)! * M!) int NumberOfResult() { int result =1; for (int i =0; i < M; i++) result *= N - i; for (int j =1; j <= M; j++) result /= j; return result; } // Get the current value // sz - array of the current result // nIndex - index of sz, 0 <= nIndex < M // nStartVal - the current minimum value, 0 <= nStartVal < N // nResultCount - index of m_result void CountRecur(int sz[M], int nIndex, int nStartVal, int& nResultCount) { if (nStartVal + M - nIndex > N) return ; for (int i = nStartVal; i < N; i++) { sz[nIndex] = i; if (nIndex == M -1) Add(sz, nResultCount); else CountRecur(sz, nIndex +1, i +1, nResultCount); } } // Save the result void Add(int* sz, int& nIndex) { memcpy(m_result[nIndex], sz, M *sizeof(int)); ++nIndex; } };
// pi : this algorith lists all the sequences of a certian array // #pragma once #include "common.h" // how many sequence for N cells int NumberOfPI(int N) { if (N <0) return0; if (N ==0) return1; int pi =1; for (int i =1; i <= N; i++) pi *= i; return pi; } // print all the sequence to a certain array // N - number of cells // PI - array of cells // selPI - index of array with initial value of 0 and maximum value of N - 1 // Index - result array // selIndex - number of result array void PrintPI(int N, int* PI, int selPI, int** Index, int selIndex) { if (N <=0) return ; int num = NumberOfPI(N - selPI -1); for (int i = selPI; i < N; i++) { swap(PI[selPI], PI[i]); PrintPI(N, PI, selPI +1, Index, selIndex); for (int j =0; j < num; j++) Index[selIndex++][selPI] = PI[selPI]; swap(PI[selPI], PI[i]); } } int PrintPITest() { // Number staticconstint N =5; // Result in all sequences int PI[N]; for (int l =0; l < N; l++) PI[l] = l +1; // Allocate the two_demension array constint num = NumberOfPI(N); int**Index; Index =newint*[num]; for (int l =0; l < num; l++) Index[l] =newint[N]; // Calculating. PrintPI(N, PI, 0, Index, 0); // Print it to cout for (int i =0; i < num; i++) { printf("%5d : ", i +1); for (int j =0; j < N; j++) printf("%2d ", Index[i][j]); printf("/n"); } // Check if there is any identical indexes. for (int i =0; i < num -1; i++) for (int j = i +1; j < num; j++) { int k =0; for (; k < N; k++) if (Index[i][k] != Index[j][k]) break; if (k == N) printf("Check ERROR!/n"); } // Release the two_demension array for (int l =0; l < num; l++) delete[] Index[l]; delete[] Index; return0; }