首页 > 代码库 > next_permutation
next_permutation
#include <iostream>
#include<algorithm>
using namespace std;
int a[5];
int main(int argc, char *argv[])
{
int i;
for(i=0;i<5;i++) a[i]=i+1;
do
{
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}
while(next_permutation(a,a+5));
return 0;
}
**************************************************************
#include <iostream>
#include <algorithm>
#include <string>
int a[10];
using namespace std;
int f(int x)
{ for(int i=2;i<x ; i++)
if( x%i==0 ) return 0;
return 1;
}
int main( )
{ int i,j,n,ok;
cin>>n;
for(i=0; i<n;i++) a[i]=i+1;
do
{ ok=1 ;
for(j=1; j<n;j++)
if(! f(a[j-1]+a[j]) ) { ok=0 ; break ; }
if(!f(a[0]+a[n-1])) ok=0 ;
if(ok==1)
{ for(i=0; i<n ; i++) cout<<a[i]<<" " ;
cout<<endl ;
}
if( a[0]!=1) return 0;
}
while(next_permutation(a,a+n));
}
*****************************************************************
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string str;
cin>>str ;
sort(str.begin(),str.end());
cout<<str<<endl;
while(next_permutation(str.begin(),str.end()))
{
cout<<str<<endl;
}
}
*************************************************
**************************************************************
#include <iostream>
#include <algorithm>
int a[10];
#include <string>
using namespace std;
int main()
{
int i,n ;
cin >> n;
for(i=0; i<n;i++) a[i]=i+1;
do
{ for(i=0; i<n;i++) cout<<a[i]<<" " ;
cout << endl;
}
while (next_permutation(a,a+n));
}
****************************************************************
#include <iostream>
#include <algorithm>
int a[10];
#include <string>
using namespace std;
int main()
{
int i,n ,j,ok;
cin >> n;
for(i=0; i<n;i++) a[i]=i+1;
do
{ ok=1;
for(j=0; j<n;j++) cout<<a[i]<<" " ;
cout << endl;
}
while (next_permutation(a,a+n));
}