题目描述
算法设计+题目分析
吐槽一下:
代码只有90分,简单的思考了5分钟,决定不纠结(因为根本就没什么算法难度)
分析题目:
题目要求做这么几件事情:
1、找出点击的窗口是哪些;
2、输出这些窗口中最顶层的。
算法:
对窗口进行排序,窗口的优先级每次点击后都改变
注意点+记录点
无
代码(90分)
环境
Visual C++6.0
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int top;//保存当前最顶层的窗口的级别
struct window{
int x1,x2,y1,y2;//窗口的两个坐标
int top;//级别,级别越高越接近顶层
int index;//窗口的编号
window(){}//用来定义窗口数组
window(int x_1, int y_1, int x_2, int y_2, int t, int d):x1(x_1), x2(x_2), y1(y_1), y2(y_2), top(t), index(d){}
}W[11];
bool cmp(window a, window b){
return a.top>b.top;
}//重载比较规则,级别大的放前面
int main(){
int n,m;
scanf("%d %d", &n, &m);
top=m-1;//级别为最顶层窗口级别
for (int i=0; i<n; i++){
int xx1,yy1,xx2,yy2;
scanf("%d %d %d %d", &xx1, &yy1, &xx2, &yy2);
W[i]=window(xx1,yy1,xx2,yy2, i,i+1);//注意同时要求保存窗口编号和级别
}
string s="IGNORED";
sort(W,W+n,cmp);//按照级别排序一下
for (int j=0; j<m; j++){
int x,y;
scanf("%d %d", &x, &y);
for(int z=0; z<n; z++){
if (x>=W[z].x1 && x<=W[z].x2 && y>=W[z].y1 && y<=W[z].y2){//找到满足条件的最顶层窗口
if (j<m-1) printf("%d\n", W[z].index);
else printf("%d", W[z].index);
W[z].top=top+1;//将当前窗口放到顶层
top=top+1;//更新全局变量为顶层窗口级别
break;
}
if (z==n-1 && j<m-1) cout<<s<<endl;
if (z==n-1 && j==m-1) cout<<s;
}
sort(W,W+n,cmp);//点击重新排序一下
}
return 0;
}
感谢链接
无