没理解到题目意思:
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+2;
int main(){
int n,m;
int ans[N];
while(scanf("%d%d",&n,&m)!=EOF){
if(m>=n){
for(int i=1;i<=n;i++)
printf("%d ",i);
printf("%d\n",m);
}
else{
for(int i=1;i<m;i++)
printf("%d ",i);
printf("%d ",m+1);
for(int i=m+2;i<=n;i++)
printf("%d ",i);
printf("%d\n",m);
}
}
return 0;
}
next_permutation函数将按字母表顺序生成给定序列的下一个较大的序列。直到整个序列为减序为止。
prev_permutation函数将按字母表顺序生成给定序列的上一个较小的序列。直到整个序列为增序为止。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e4+2;
int x[MAXN];
int main(){
int n,m,i;
while(cin>>n>>m){
for(i=0;i<n;i++)
x[i]=i+1;
for(i=1;i<m;i++)
next_permutation(x,x+n);
for(i=0;i<n;i++){
cout<<x[i];
if(i!=n-1)
cout<<' ';
else
cout<<endl;
}
}
return 0;
}
即第1到6小序列:
1 2 3 4 5 6
1 2 3 4 6 5
1 2 3 5 4 6
1 2 3 5 6 4
1 2 3 6 4 5
1 2 3 6 5 4
11 1
1 2 3 4 5 6 7 8 9 10 11
11 2
1 2 3 4 5 6 7 8 9 11 10
11 3
1 2 3 4 5 6 7 8 10 9 11
11 4
1 2 3 4 5 6 7 8 10 11 9
11 5
1 2 3 4 5 6 7 8 11 9 10
11 6
1 2 3 4 5 6 7 8 11 10 9
11 7
1 2 3 4 5 6 7 9 8 10 11
11 8
1 2 3 4 5 6 7 9 8 11 10
11 9
1 2 3 4 5 6 7 9 10 8 11
11 10
1 2 3 4 5 6 7 9 10 11 8
11 11
1 2 3 4 5 6 7 9 11 8 10
即第1到11小序列:
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 11 10
1 2 3 4 5 6 7 8 10 9 11
1 2 3 4 5 6 7 8 10 11 9
1 2 3 4 5 6 7 8 11 9 10
1 2 3 4 5 6 7 8 11 10 9
1 2 3 4 5 6 7 9 8 10 11
1 2 3 4 5 6 7 9 8 11 10
1 2 3 4 5 6 7 9 10 8 11
1 2 3 4 5 6 7 9 10 11 8
1 2 3 4 5 6 7 9 11 8 10