CCF认证201403前两题:相反数、窗口

本文介绍了两个Java程序示例,一个是通过数组实现的窗口点击处理算法,另一个是计算整数相反数对的数量。使用了Scanner类进行输入,并通过数组和自定义类实现了窗口层级管理和相反数对的查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

201403-1 相反数:

package exam201403;

import java.util.Scanner;

public class Task1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        int[] num = new int[n];
        for(int i=0; i<n; i++){
            num[i] = sc.nextInt();
            for(int j=0; j<i; j++){
                if(num[i] == -num[j]){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

201403-1 窗口:
利用数组实现:

package exam201403;

import java.util.Scanner;

public class Task2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();

        int[] X1 = new int[m];
        int[] Y1 = new int[m];
        int[] X2 = new int[m];
        int[] Y2 = new int[m];
        int[] tag = new int[m]; //记录初始编号
        int[] clickX = new int[n];
        int[] clickY = new int[n];

        int[]temp = new int[5];

        for(int i=m-1; i>=0; i--){ //后输入的在顶层,顶层放在数组前面
            X1[i] = sc.nextInt();
            Y1[i] = sc.nextInt();
            X2[i] = sc.nextInt();
            Y2[i] = sc.nextInt();
            tag[i] = m-i;
        }
        for(int i=0; i<n; i++){
            clickX[i] = sc.nextInt();
            clickY[i] = sc.nextInt();
        }
        for(int i=0; i<n; i++){
            boolean flag = true;
            for(int j=0; j<m; j++){
                if(clickX[i]>=X1[j] && clickX[i]<=X2[j] && clickY[i]>=Y1[j] &&clickY[i]<=Y2[j]){
                    System.out.println(tag[j]);

                    temp[0] = X1[j];
                    temp[1] = Y1[j];
                    temp[2] = X2[j];
                    temp[3] = Y2[j];
                    temp[4] = tag[j];
                    for(int k=j; k>0; k--){ //把点击的窗口放在最前,其他顺序不变(前排后移)
                        X1[k] = X1[k-1];
                        Y1[k] = Y1[k-1];
                        X2[k] = X2[k-1];
                        Y2[k] = Y2[k-1];
                        tag[k] = tag[k-1];
                    }
                    X1[0] = temp[0];
                    Y1[0] = temp[1];
                    X2[0] = temp[2];
                    Y2[0] = temp[3];
                    tag[0] = temp[4];

                    flag = !flag;
                    break;
                }
            }
            if(flag){
                System.out.println("IGNORED");
            }
        }

    }
}

更简单的方法,利用 容器ArrayList和静态类 实现:

package exam201403;

import java.util.ArrayList;
import java.util.Scanner;

public class Task2_list {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<loc> al = new ArrayList<loc>();
        Scanner sc = new Scanner(System.in);
        int m=0, n=0;
        int x1, y1, x2, y2;
        int i, j;

        m = sc.nextInt();
        n = sc.nextInt();
        for(i=0; i<m; i++){ //先输入的窗口在底部
            x1 = sc.nextInt();
            y1 = sc.nextInt();
            x2 = sc.nextInt();
            y2 = sc.nextInt();

            al.add(new loc(x1, y1, x2, y2, i+1));
        }

        String[] print = new String[n];

        for(i=0; i<n; i++){
            x1 = sc.nextInt();
            y1 = sc.nextInt();

            j = m-1;
            for(; j>=0; j--){  //先输入的窗口在尾部,要从顶部开始找,下标越大越靠前
                int tag = al.get(j).find(x1, y1);
                if(tag != -1){
                    print[i] = tag+""; 
                    al.add(al.get(j));
                    al.remove(j);
                    break;
                }
            }
            if(j == -1){
                print[i] = "IGNORED";
            }
        }

        for(i=0; i<n; i++){
            System.out.println(print[i]);
        }
    }

    static class loc{
        int x1, x2, y1, y2, tag;
        public loc(int x1, int y1, int x2, int y2, int tag) {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
            this.tag = tag;
        }

        public int find(int x, int y){
            if(x>=x1 && x<=x2 && y>=y1 && y<=y2){
                return tag;
            }
            return -1;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值