文章

56

粉丝

66

获赞

56

访问

3.6k

头像
二元组整数 (搜索和C++ set)题解:
P1024 贵州大学机试题
发布于2024年4月20日 23:34
阅读数 80

C++ set去重:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;

const int N = 30;
set<PII> res;
int a[N];
int n;

int main()
{
	cin >> n;
	
	for(int i = 0; i < n; i ++)
		cin >> a[i];
	
	sort(a, a + n);
	
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++)
			if(i != j)
				res.insert({a[i], a[j]});
			
	for(auto t : res)
		cout << "(" << t.first << "," << t.second << ")"<< endl;
		
	return 0;	
}

dfs搜索: 

#include<bits/stdc++.h>
using namespace std;

const int N = 110;
int a[N];
bool st[N];
int n;
vector<int> res;

void dfs(int u)
{
	if(u == 2)
	{
		printf("(%d,%d)\n", res[0], res[1]);
		
		return ;
	}
	
	for(int i = 0; i < n; i ++)
	{
	    if(i > 0 && a[i] == a[i - 1] && !st[i - 1]) continue;  //剪枝去重
		if(!st[i])
		{
			res.push_back(a[i]);
			st[i] = true;
			
			dfs(u + 1);
	...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发