Create a full array of 1~n

Source: Internet
Author: User

Enter a positive integer n, which is the full rank of the output n.

Sample Input 1:

3

Sample Output 1:

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

Analysis:

Output all permutations in sequence from small to large in dictionary order.

(Dictionary order: the dictionary order size relationship of two sequences is equivalent to the size relationship at the first different position from the beginning of a sequence)

Using array A to save the number in the arrangement, the set S represents the remaining number.

Then there are

Method 1:

1. Pseudo-code:

int Dfs (A,S,CUR) {//finds the CUR number in the arrangement.

If (s== null) outputs this arrangement;

Else

Take the element I in the set S;

a[cur]=i;//save in a array

DFS (A,S,CUR+1);

}

s set can be omitted, directly denoted by a, if I exist in a[1]~a[cur-1], then I have been used, otherwise the description I in the set S.

Because you want to start with the first number, you first call DFS (1)//cur=1

Source:

#include <cstring>

using namespace Std;

int a[100],s, n;;

void dfs (int cur) {///function has no return value, so the type is void

if (cur==n+1) {

for (int i=1;i<n;i++) cout<<a[i]<< "";

cout<<a[n]<<endl;

s++;

}

else for (int i=1;i<=n;i++) {

int ok=1;

for (int j=1;j<cur; j + +)//Because we are currently looking for the cur number, I compare it with the cur-1 number I have found earlier

if (a[j]==i) ok=0;

if (OK) {a[cur]=i;d fs (cur+1);}

}

}

int main () {

memset (A,0,sizeof (a));

cin>>n;

DFS (1);

cout<<s<<endl;

return 0;

}

Thinking:

(1) Global variables and local variables;

For example a array and variable n,s are used in both main and DFS, so defined as a global variable, cur is only used in the currently called function, so it is defined as a local variable.

(2) How the function does not return a value when it is processed.

function has no return value, the function type is void, can have no return statement, or can have, but there is no expression after

return;

2.

Pseudo code:

int DFS (cur) {//has found cur numbers and is ready to find the next one.

If (s== null) outputs this arrangement;

Else

Take the element I in the set S;

a[cur+1]=i;//save in a array

DFS (cur+1);

}

0 numbers were found initially, so Dfs is called first (0)

1#include <iostream>2#include <cstring>3 using namespacestd;4 inta[ -],s=0, N;5 voidDfsintcur) {//function has no return value when type is void6     if(cur==N) {7          for(intI=1; i<n;i++) cout<<a[i]<<" ";8cout<<a[n]<<Endl;9s++;Ten     } One     Else A           for(intI=1; i<=n;i++){ -               intok=1; -                for(intj=1; j<=cur;j++) the                     if(a[j]==i) ok=0; -               if(OK) { -a[cur+1]=i;  -DFS (cur+1);  +             } -           }       +}
View Code
1#include <iostream>2#include <cstring>3 using namespacestd;4 inta[ -],s, n;;5 voidDfsintcur) {6     if(cur==N) {7          for(intI=1; i<n;i++) cout<<a[i]<<" ";8cout<<a[n]<<Endl;9s++;Ten     return; One     } A       for(intI=1; i<=n;i++){ -           intok=1; -            for(intj=1; j<=cur;j++) the                 if(a[j]==i) ok=0; -           if(OK) { -a[cur+1]=i;  -DFS (cur+1);  +         } -      }       +}
View Code

Create a full array of 1~n

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.