当前位置:网站首页>HZOJ #236. Recursive implementation of combinatorial enumeration

HZOJ #236. Recursive implementation of combinatorial enumeration

2022-07-07 12:50:00 Duange

subject :236. Recursive implementation of combinatorial enumeration

Subject portal :236 topic
Title Description
​ from 1−n1−n this nn Random selection of integers mm individual , The numbers in each scheme are arranged from small to large , Output all possible options in dictionary order .

Input
​ Enter two integers n,mn,m.(1≤m≤n≤10)(1≤m≤n≤10)

Output
​ Each line has a set of schemes , The two numbers in each group are separated by spaces .

​ Note that there is no space after the last number in each line .
The sample input
3 2
Sample output

1 2
1 3
2 3

The sample input 2

5 3

Sample output 2

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

Code

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int n, m, num[15];

void func(int start, int left, int ind)
{
	if (left == 0)
	{
		for (int i = 0; i < m; i++)
		{
			if (i) cout << " ";
			cout << num[i];
		}
		cout << endl;
		return;
	}

	for (int i = start; i <= n; i++)
	{
		num[ind] = i;
		func(i + 1, left - 1, ind + 1);
	}
}

int main()
{
	cin >> n >> m;
	func(1, m, 0);
	return 0;
}
原网站

版权声明
本文为[Duange]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071036348382.html