CCF 窗口 Java实现

今天看到了一道CCF的题目,感觉挺有意思的,题目如下:

问题描述   
在某图形操作系统中,有 N个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域。窗口的边界上的点也属于该窗口。窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的内容。
  当你点击屏幕上一个点的时候,你就选择了处于被点击位置的最顶层窗口,并且这个窗口就会被移到所有窗口的最顶层,而剩余的窗口的层次顺序不变。如果你点击的位置不属于任何窗口,则系统会忽略你这次点击。
  现在我们希望你写一个程序模拟点击窗口的过程。
输入格式   
输入的第一行有两个正整数,即 N 和 M。(1 ≤ N ≤ 10,1 ≤ M≤ 10)   
接下来 N 行按照从最下层到最顶层的顺序给出 N 个窗口的位置。 每行包含四个非负整数 x1, y1, x2,y2,表示该窗口的一对顶点坐标分别为 (x1, y1) 和 (x2, y2)。保证 x1 < x2,y1

import java.util.Scanner;
public class Window {

    public static void main(String[] args){
        System.out.println("please input the N and M");
        Scanner scanner=new Scanner(System.in);
        int N=scanner.nextInt();
        int M=scanner.nextInt();

        int[][] coord=new int[N][5];

        System.out.println("please input the coordinates");
        int k=1;
        for (int i = 0; i < N; i++) {
            coord[i][0]=k;
            k++;
            for (int j = 1; j < 5; j++) {
                coord[i][j]=scanner.nextInt();

            }
        }

        System.out.println("please input M");
        int[] result=new int[M];
        int[][] temp=new int[1][5];
        int x,y;
        for (int i = 0; i <M; i++) {
            x=scanner.nextInt();
            y=scanner.nextInt();
            for (int j = N-1; j>=0; j--) {
                if (x>=coord[j][1]&&x<=coord[j][3]&&y>=coord[j][2]&&y<=coord[j][4]) {
                    result[i]=coord[j][0];

                    temp[0][0]=coord[j][0];
                    temp[0][1]=coord[j][1];
                    temp[0][2]=coord[j][2];
                    temp[0][3]=coord[j][3];
                    temp[0][4]=coord[j][4];

                    for(int t=j;t<N-1;t++){

                        coord[t][0]=coord[t+1][0];
                        coord[t][1]=coord[t+1][1];
                        coord[t][2]=coord[t+1][2];
                        coord[t][3]=coord[t+1][3];
                        coord[t][4]=coord[t+1][4];

                    }

                    coord[N-1][0]=temp[0][0];
                    coord[N-1][1]=temp[0][1];
                    coord[N-1][2]=temp[0][2];
                    coord[N-1][3]=temp[0][3];
                    coord[N-1][4]=temp[0][4];

                    break;
                }else {
                    result[i]=0;
                }
            }
        }

        for (int i = 0; i < M; i++) {
            if (result[i]==0) {
                System.out.println("IGNORED");
            }else {
                System.out.println(result[i]);
            }
        }

        scanner.close();
    }

}

标记为原创的博客均为本人辛苦码字得到,谢绝抄袭,欢迎转载,转载请注明出处。

发布了45 篇原创文章 · 获赞 8 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/iNiegang/article/details/47030435