next_permutation实现

#include <iostream>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
 
void swap(int &a ,int &b)
{
    int temp=a;
    a=b;
    b=temp;
}
void permutation(int arr[],int b[],bool f[],int m,const int n)
{
    if (m==n)
    {
        for (int i =0;i<n;i++)cout<<b[i]<<" ";
        cout<<endl;
    }
     else
     {
         for (int i = 0;i<n;i++)  //该位置从最低数试起(因为要得到字典序最小的)
         {
             if (f[i])continue;
             f[i]=true;
             b[m]=arr[i];
             permutation(arr,b,f,m+1,n);
             f[i]=false;
         }
     }
}
int main ()
{
    int  a[]={1,2,3};
//     reverse(a,a+3);
    int b[3];
    bool flag[3];
    permutation (a,b,flag,0,3);    
    return 0;
} 

 

posted @ 2022-02-27 10:32  兮何其  阅读(20)  评论(0)    收藏  举报