1144: 组合的输出

1144: 组合的输出

Description

排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r个数。 现要求你不用递归的方法输出所有组合。 例如n=5,r=3,所有组合为: l 2 3 l 2 4 1 2 5 l 3 4 l 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5

Input

一行两个自然数n、r(1< n < 21,1<= r <=n)。

Output

所有的组合,每一个组合占一行且其中的元素按由小到大的顺序排列,每个元素占三个字符的位置,所有的组合也按字典顺序。

Sample Input

5 3


对于STL排列函数next_permutation(a , a + n)的应用 

n 为选取 n 个数进行排列求下一个排列,且下一个排列比当前大但按字典序是最小的排列

例如:例题选取下标 3 4 5 作为标记 1 ,其余为 0 。则我们可以先输出排列数为 0 的(下标数 +1)   (0 + 1 =)1   (1 + 1 = )2   (2 + 1 = )3 (即当前数组为 0 0 0 1 1 1

输出后进入排列。。可以排列出的数字为 0 0 1 0 1 1,因为作为数字看001011  比未排前的大但却又是最小。

因此,根据数组里数字为 0 的数,可以输出其下标加 1 ,然后继续循环


#include<cstdio>
#include<cstring>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
#endif
	int n , m , a[25];
	cin >> n >> m ;
	memset(a,0,sizeof(a));
	for(int i = n; i >= m ; i--)
	{
		a[i]=1;
	}
	do
	{
		for(int i = 0; i < n; i++)
		{
			if(!a[i])
			{
				printf("%3d",i+1);
			}
		}
		cout << endl;
	}
	while(next_permutation(a , a + n));

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值