Recursive solutions to several types of permutation and combination problems (2. Full Permutation)

Source: Internet
Author: User

If the search depth is deep or the depth is not fixed, you cannot use the enumeration method to set the layers of nested loops. In this case, you can use recursion to complete the search task. Recursion is a common algorithm, and it is another Implementation Method of search. If a function or process is used in algorithm design to directly or indirectly call itself to solve the problem, this method is called a recursive algorithm. A recursive algorithm must have one or more definite recursive termination conditions.

Sample Input

3

1 2 3

Sample output

123

132

213

231

312

321

# Include <stdio. h> # include <string. h> const int maxn = 11; int N; int used [maxn]; // mark the array int mat [maxn]; // store the array int num [maxn]; // output array void solve (int l) {If (L> = N) {for (INT I = 0; I <n; ++ I) printf ("% d", num [I]); puts (""); Return ;}for (INT I = 0; I <n; ++ I) {If (! Used [I]) {used [I] = 1; num [l] = mat [I]; solve (L + 1); used [I] = 0 ;}}} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <n; ++ I) scanf ("% d", mat + I); memset (used, 0, sizeof (used); solve (0);} return 0 ;}


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.