CCF计算机软件能力认证试题:201403-2 窗口

在这里插入图片描述

解题思路:

1,由题意可知模拟窗口顺序是解决问题的关键,并且每次点击完后需要对窗口顺序进行调整,我使用的是一个简单的链表(顺便练习一下简单的链表操作),采用尾插法刚刚可以表示窗口的层数位置,并且我觉得这样的话调整窗口顺序时比较方便,但由于窗口顺序的设置当时没完全读懂(理解水平有限),所以花了些时间去想。

测试代码:

#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;

typedef struct list{
	int index;
	int x1, y1, x2, y2;
	struct list *next;
}*Crd,crd;

void freeMem(Crd Head) {//简单递归
	if (Head != NULL) {
		freeMem(Head->next);
	}
	free(Head);
}
int main() {
	int N, M;//含义与题同
	Crd Head = (Crd)malloc(sizeof(crd));
	Head->index = 0;
	Head->next = NULL;
	Head->x1 = Head->x2 = Head->y1 = Head->y2 = -1;
	cin >> N >> M;
	for (int i = 0; i < N; ++i) {
		Crd tmp = (Crd)malloc(sizeof(crd));
		cin >> tmp->x1 >> tmp->y1 >> tmp->x2 >> tmp->y2;
		tmp->index = i+1;//表示层数
		//头插法
		tmp->next = Head->next;
		Head->next = tmp;
	}
	
	vector<int> res;//存入负数时,表示输出IGNORED
	for (int i = 0; i < M; ++i) {
		int cx, cy;
		cin >> cx >> cy;
		int count = 0;
		//直接处理将结果存入一个vector
		Crd temp = Head->next;
		while (temp != NULL) {
			if (cx >= temp->x1&&cx <= temp->x2&&cy >= temp->y1&&cy <= temp->y2) {
				res.push_back(temp->index);
			//调整窗口顺序
				Crd Jude = Head;
				while (Jude->next != temp ) {
					Jude = Jude->next;
				}
				if (Jude->next == temp && Head->next != temp) {
					Jude->next = temp->next;
					temp->next = Head->next;
					Head->next = temp;
				}
				break;
			}	
				temp = temp->next;
				count++;
		}
		if (N == count) {
			res.push_back(-1);
		}
	}	
	for (int i = 0; i < res.size(); ++i) {
		if (-1 == res.at(i)) {
			cout << "IGNORED" << endl;
		}
		else {
			cout << res.at(i) << endl;
		}
	}
	freeMem(Head);//释放malloc获取的空间
	system("pause");
	return 0;
}

欢迎评论,共同进步。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值