西电复试之——ccf 201912-2 回收站选址

在这里插入图片描述

STL

#include<map>
#include<iostream>
using namespace std;
const int maxn = 1010;
int cnt[5] = { 0 };
int main() {
	int n;
	cin >> n;
	map<pair<int, int>, int> ps;
	pair<int, int> p[maxn];
	for (int i = 0; i < n; i++) {
		cin >> p[i].first >> p[i].second;
		ps[p[i]] = 1;
	}
	for (int i = 0; i < n; i++) {
		int x = p[i].first;
		int y = p[i].second;
		if (ps[make_pair(x - 1, y)] && ps[make_pair(x + 1, y)] 
			&& ps[make_pair(x, y - 1)] && ps[make_pair(x, y + 1)]) {
			cnt[ps[make_pair(x - 1, y - 1)] + ps[make_pair(x - 1, y + 1)] +
				ps[make_pair(x + 1, y - 1)] + ps[make_pair(x + 1, y + 1)]]++;
		}
	}
	for (int i = 0; i < 5; i++) {
		if (i == 5) {
			cout << cnt[5];
			continue;
		}
		cout << cnt[i] << endl;
	}
	return 0;
}

结构体解法

#include<iostream>
using namespace std;

const int maxn = 1010;
int num[5] = { 0 };
struct point {
	int x;
	int y;
}p[maxn];
int main() {
	int n;
	cin >> n;
	//初始化点
	for (int i = 0; i < n; i++) {
		cin >> p[i].x >> p[i].y;
	}
	for (int i = 0; i < n; i++) {
		int temp = 0;
		int x = p[i].x, y = p[i].y;
		for (int j = 0; j < n; j++) {
			if (x == p[j].x && y + 1 == p[j].y)  //上 
				temp++;
			if (x == p[j].x && y - 1 == p[j].y)  //下 
				temp++;
			if (x - 1 == p[j].x && y == p[j].y)  //左
				temp++;
			if (x + 1 == p[j].x && y == p[j].y)  //右 
				temp++;
			if (temp == 4)
				break;
		}
		if (temp == 4) {
			int count = 0;
			//统计分数个数 
			for (int j = 0; j < n; j++)
			{
				if (x + 1 == p[j].x && y + 1 == p[j].y)  //右上 
					count++;
				if (x + 1 == p[j].x && y - 1 == p[j].y)  //右下 
					count++;
				if (x - 1 == p[j].x && y - 1 == p[j].y)  //左下 
					count++;
				if (x - 1 == p[j].x && y + 1 == p[j].y)  //左上 
					count++;
			}
			num[count]++;
		}
	}
	for (int i = 0; i < 5; i++) {
		cout << num[i] << endl;
	}
	return 0;
}
原创文章 35 获赞 17 访问量 1280

猜你喜欢

转载自blog.csdn.net/qq_41436493/article/details/105611943