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

HZOJ #235. Recursive implementation of exponential enumeration

2022-07-07 12:51:00 Duange

subject :235. Recursive implementation of exponential enumeration

Subject portal :235 topic
Title Description
​ from 1−n1−n this nn Randomly select any number of integers , The numbers in each scheme are arranged from small to large , Output all possible options in dictionary order .

Input
​ Enter an integer nn.(1≤n≤10)(1≤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 .

sample input

3

Sample output

1
1 2
1 2 3
1 3
2
2 3
3

The sample input

4

Sample output

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

Code 1

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

using namespace std;

int n, num[15];

void print(int ind)
{
	for (int i = 0; i <= ind; i++)
	{
		if (i) cout << " ";
		cout << num[i];
	}
	cout << endl;
}

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

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

Code 2

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

using namespace std;

int n, num[15],cnt;

void print()
{
	for (int i = 0; i <= cnt; i++)
	{
		if (i) cout << " ";
		cout << num[i];
	}
	cout << endl;
}


void func(int start)
{
	for (int i = start; i <= n; i++)
	{
		num[cnt] = i;
		print();
		cnt++;
		func(i + 1);
		cnt--;
	}
}

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

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