洛谷 P1157 组合的输出(组合型)

题目链接:P1157 组合的输出 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

组合问题要规定枚举的顺序,不能重复,所有dfs()传两个参数,第一个参数是枚举到哪个位置,第二个参数是从哪个数开始枚举。

#include 
#include 
#include 

using namespace std;

const int N = 25;

int n, r;
int arr[N];

void dfs(int x, int start)
{
	if (x > r)
	{
		for (int i = 1; i <= r; i++)
			cout << setw(3) << arr[i];
		cout << endl;

		return;
	}

	for (int i = start; i <= n; i++)
	{
		arr[x] = i;
		dfs(x + 1, i+1);
		arr[x] = 0;
	}
}

int main()
{ 
	cin >> n >> r;
	dfs(1, 1);
	return 0;
}

你可能感兴趣的:(算法,c++,深度优先)