C + + recursive method for full array

Source: Internet
Author: User

#include <iostream>using namespacestd;voidPermintList[],intKintm);//StatementvoidPermintList[],intKintM//called{    if(k==m)//equal to the output, always output the last layer. {        for(intj=0; j<=m;j++) {cout<<List[j]; } cout<<Endl; }    Else    {      for(inti=k;i<=m;i++) {swap (list[k],list[i]);//On The first floor, change with yourself first. 1 and 1 for, 2 and 2 for, 3 is equal then output. Output back, 2 and 3 to replace. Perm (list,k+1, M); Swap (list[k],list[i]);//Change The order of the previous changes back to prevent the order from changing.      }    }}intMain () {//Char a[7]= "123456";    inta[3]={0,1,2}; Perm (A,0,2); System ("Pause"); return 0;}

Logical Order:

The first layer Loops 3 times:

K=0,i=k=0,list[0] and list[0] Exchange. And then call k+1=1,2 recursively.

Go to the second layer and loop 2 times:

K=1,I=K=1,LIST[1] and List[1] exchange, and then recursively call k+1=2,2

At this time equal, output result, 0,1,2 (equivalent to No loop or loop 1 times)

And then back to the second layer, the 2nd time of the second layer.

K=1,I=K+1=2,LIST[1] and list[2] exchange, and then recursively call k+1=2,2

Equal at this time, output 0,2,1

Then go back to the first layer, cycle the first layer for the 2nd time, followed by the same process.

C + + recursive method for full array

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.