Algorithm: C + + permutation combination

Source: Internet
Author: User


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.