题目描述
输入一个序列,输出其下一个字典序的排列
序列的下一个排列是指其下一个字典序更大的排列。更正式地,如果序列的所有排列根据其字典顺序从小到大排列在一个容器中,那么序列的下一个排列就是在这个有序容器中排在它后面的那个排列。如果不存在下一个更大的排列,那么这个序列必须重排为字典序最小的排列(即,其元素按升序排列)
如:输入1 2 3,
下一个为1 3 2,
下一个为2 1 3,
下一个为2 3 1,
下一个为3 1 2,
下一个为3 2 1(最大的字典序排列)
下一个为1 2 3(重新返回最小的字典序排列)
......
输入有多种类型:
C:表示char型数据
I:表示int型数据
D:表示double型数据
S:表示字符串型数据
输入
第1行:一个整数n,表示测试次数
以下n行:每行为 C/I/D/S k ...k个数据(用空格分隔)
输出
对每个测试数据输出其下一个字典序排列
输入样例1<-复制
5
C 6 a b d c f e
I 4 1 2 3 4
D 2 4.3 2.1
S 4 ho ha he he
I 6 1 2 3 4 6 4
C 6 a b d c f e
I 4 1 2 3 4
D 2 4.3 2.1
S 4 ho ha he he
I 6 1 2 3 4 6 4
输出样例1
a b d e c f
1 2 4 3
2.1 4.3
ho he ha he
1 2 3 6 4 4
这道题我用了一个不怎么用的函数next_permutation,用来进行下一个排列,需要加上头文件 #include<algorithm>,其次是需要用上函数模板
#include<iostream>
#include<algorithm>
using namespace std;
template <class T>
void set_and_display(T *a,int n){
cin>>n;
a=new T[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
next_permutation(a,a+n);
for(int i=0;i<n;i++){
if(i<n-1)
cout<<a[i]<<" ";
else{
cout<<a[i];
}
}
}
int main(){
int t;cin>>t;
char type;
while(t--){
cin>>type;
if(type=='C'){
char *a;int n;
set_and_display(a,n);
}else if(type=='I'){
int *a,n;
set_and_display(a,n);
}else if(type=='D'){
double *a;
int n;
set_and_display(a,n);
}else if(type=='S'){
string *a;int n;
set_and_display(a,n);
}
if(t!=0){
cout<<endl;
}
}
return 0;
}