题目描述
求下一个排列
可以直接用stl中的next_premutation
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char c[105];
int main(){
while(cin>>c){
if(strcmp(c,"#")==0)
break;
if(next_permutation(c,c+strlen(c)))
cout<<c<<endl;
else
cout<<"No Successor"<<endl;
}
}
手动排序
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char c[105];
int l;
int get(){
int i=l-1;
while(i>0&&c[i-1]>=c[i])
{
i--;
}
if(!i)
return 0;
int mp=i;
for(int j=i+1;j<l;j++)
{
if(c[j]<=c[i-1]) continue;
if(c[j]<c[mp]) mp=j;
}
swap(c[mp],c[i-1]);
sort(c+i,c+l);
return 1;
}
int main(){
while(cin>>c){
if(strcmp(c,"#")==0)
break;
l=strlen(c);
if(get())
cout<<c<<endl;
else
cout<<"No Successor"<<endl;
}
}