C -- full Arrangement Implementation (recursive method) can be understood by fools

Source: Internet
Author: User

Assume that the array contains n elements, extract each element in the array as a Header element, and then arrange all elements except the first element in the array, in this way, all elements in the array are arranged in full order. [This sentence is the focus !] For example, the full arrangement of and 3 is the full arrangement starting with and 3, respectively. The full arrangement starting with 1 is the full arrangement of 2, 3, and (2, 3) is the full arrangement starting with 2 and 3, respectively. R (n1, n2, n3.... nn) can be simplified to n1, n2, n3 ...... Start with a full arrangement. That is, n1R1 (n2, n3.... nn), n2R2 (n1, n3.... nn), n3R3 (n1, n2,... nn )...... NnR (n1, n2, n3....) Where R1 (n2, n3.... nn) can continue in R mode ...... And so on. Copy the Code # include <iostream> using namespace std; // index, which indicates the position of the subseries in the original series during recursion. // For example, a [] = {1, 2, 3 }, the LENGTH of the original sequence is 3, // If you recursion to a certain step, index = 1, num = 2, which indicates that the following table of the original sequence is 1, the LENGTH starts from 2 (that is, from the sequence 2 and 3), and the full arrangement of the subsequence/(2 and 3) is the LENGTH of the original array. This will not change. Void permutation (int values [], int index, int num, int LENGTH) {int I = 0; if (num = 0) // a full arrangement has been found, show output {for (I = 0; I <LENGTH; ++ I) {cout <values [I] <"" ;}cout <endl; return ;} for (I = 0; I <num; I ++) {swap (values [index + I], values [index]); // The number of the index and the number of the index + I are exchanged to ensure that the first element of the Self-series is exchanged and arranged with each element in the sub-series. Permutation (values, index + 1, num-1); // swap (values [index], values [index + I]); // The inverse operation of the first statement in the for loop, the purpose of which is to make the array look back, // The purpose of doing so is that the arrangement will not produce duplicate results.} Return;} int main () {int values [] = {, 5}; permutation (values, 3); cout <endl; for (int I = 0; I <3; ++ I) {cout <values [I] <'';} cout <endl; return 0;} copy the code

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.