CCF-201912-2 回收站选址 C语言实现 简洁易懂

题目

在这里插入图片描述

解题思路

因为:
1:x, y有很大的测试案列,二维数组浪费空间,并且可能无法分配那么大的空间。
2:有负数坐标。
所以:
1:使用链表
2:链表按x排序存储

代码

注意,在CCF答题栏选C++

因为for(int i = 0; i < 5; i++)这样的语句C编译不通过。

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
	int x, y;
	struct Node *next;
} Node, *List; 
void insertSort(Node *head, int x1, int y1) {
	//有头节点 
	Node *node = (Node *)malloc(sizeof(Node));
	node->x = x1;
	node->y = y1;
	Node *p = head;
	while (p->next != NULL && p->next->x < x1) p = p->next;
	node->next = p->next;
	p->next = node;
}
//检查x,y坐标是否在链表中
int check(Node *node, int x1, int y1) {
	//无头节点 
	while (node != NULL && node->x < x1) node = node->next;
	while (node != NULL && node->x == x1) {
		if (node->y == y1) return 1;
		node = node->next;
	}
	return 0;
}
int main() {
	int n, x1, y1;
	scanf("%d", &n);
	Node *head = (Node *)malloc(sizeof(Node));
	head->next = NULL;
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &x1, &y1);
		insertSort(head, x1, y1);
	}
	int grades[5] = {0};
	Node *p = head->next;
	while (p) {
		x1 = p->x;
		y1 = p->y;
		if (check(head->next, x1 - 1, y1) && check(p->next, x1 + 1, y1) 
		&& check(head->next, x1, y1 - 1) && check(head->next, x1, y1 + 1)) {
			int g = check(head->next, x1 - 1, y1 + 1) + check(p->next, x1 + 1, y1 + 1) 
			+ check(head->next, x1 - 1, y1 - 1) + check(p->next, x1 + 1, y1 - 1);
			grades[g]++;
		}
		p = p->next;
	}
	for (int i = 0; i < 5; i++) 
		printf("%d\n", grades[i]);
	return 0;
}

测评

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值