HDU 1027 Ignatius and the Princess II

1027

没理解到题目意思:

#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;
}

6
即第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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值