P1706 Full arrangement problem (Luogu)

Original title portal

Insert picture description here
Idea: Water problem, first assign the first n items of the natural number sequence starting from 1 to the array, then calculate the total array number m of the array, and finally perform the m loop output and the next full array (using STL in The next_permutation(a,a+n) method)

Code reference

#include <bits/stdc++.h>
using namespace std;
int a[10],n,m=1;
int main()
{
    
    
    cin>>n;
    for(int i = 0;i < n;i++)
            a[i] = i+1;
    //计算n!,表示一个有n元素的数组的全排列数
    for(int i = 1;i <= n;i++)
        m *= i;
    for(int i = 0;i < m;i++){
    
    
        for(int j = 0;j < n;j++)
            cout<<"    "<<a[j];
        cout<<endl;
        next_permutation(a,a+n);//进行下一次全排列
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Bertil/article/details/106818196