蒜头君破案

题目:

在这里插入图片描述
在这里插入图片描述
代码如下:

#include<bits/stdc++.h>
using namespace std;
#define MAX 200005
struct People
{
	int height,weight,age;
	People() {}
	People(int h,int w,int a):height(h),weight(w),age(a) {}
	bool operator < (const People & p) const 
	{
		if(height != p.height) return height < p.height;
		if(weight != p.weight) return weight < p.weight;
		return age < p.age;
	}
}people; 
int main()
{
	int n,m,h,w,a;
	map<People,int> q;
	cin >> n >> m;
	for(int i = 0;i < n;i++){
		cin >> h >> w >> a;
		people = People(h,w,a);
		q.insert(make_pair(people,2));
	}
	for(int i = 0;i < m;i++){
		cin >> h >> w >> a;
		people = People(h,w,a);
		if(q[people]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

这道题用map做,map的两个值分别代表一个结构体和一个整型,输入n个本市人的3个特征,随后将其赋值位2,在查询时只要值是2的就是本市人输出yes,不是的输出no。另外,在结构体中需要重载 < 符号。比交的时候应该把结构体中三个数都依次作比较,第一次就因为只比较一个数所以导致错误。

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/87279937
今日推荐