Title: Given 1-n numbers, permutations and combinations.
Solution: Recursive. The first number has n choices, the second number has a n-1 selection, and the output is recursively arranged sequentially. Use an array to represent n digits, and use the number 0.
Implementation language: C + +
#include <iostream>using namespace std;/******************************************************************* * Num: Array to be arranged count: Total number of arrays NUMC: array iuse: Already arranged inull: number of 0 sum: Total number of permutations *//*********************************** /template <class t>void combinenum (t *num, const int count, T *numc, int iuse, int* sum) {int inull = 0; T *newnum = new T[count];for (int i = 0; i < count; ++i) {memcpy (newnum, NUM, count), if (newnum[i] = = 0) {++inull;if (iNu ll = = count) {for (int i = 0; i < count; ++i) {cout << numc[i];} cout << endl;++ (*sum); return;} Continue;} Numc[count-iuse] = Newnum[i];newnum[i] = 0; Combinenum (Newnum, Count, NUMC, iUse-1, sum);} Delete[] Newnum;} int main () {int sum = 0;const int count = 4;char Num[count], pnum[count];for (int i = 0; i < count; ++i) {Num[i] = i + ' 1 ';} Combinenum<char> (Num, Count, Pnum, Count, &sum); cout << sum: << sum << endl;sum = 1;for (in t i = 1; I <= count; ++i) {sum *= i;} cout << "sum:" << sum << endl;return 0;}
Output:
text/Shing original Reprint please indicate the source http://blog.csdn.net/yxstars/article/details/41516105
Algorithm: C + + permutation combination