java写的,报WA,在idea里结果看起来正确,求dl指点指点
查看原帖
java写的,报WA,在idea里结果看起来正确,求dl指点指点
1444408
AnPotato楼主2024/9/17 18:30
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] res = new int[n];   //存放每次查询的结果
        int count = 0;  //记录最终要展示的数据中结果的个数
        ArrayList<data> data = new ArrayList<>();  //存放所有数据,包括这个数据的id和值
        for (int i = 0;i < n;i++){
            int type = sc.nextInt();  //选择操作的类型

            if(type == 1){  //执行插入操作
               int num = sc.nextInt();   //本次插入几组数据
                for(int j = 0;j < num;j++){
                    int id = sc.nextInt();   //数据的id
                    int value = sc.nextInt();
                    data dt = new data(id,value);
                    data.add(dt);
                }
            } else if (type == 2) {   //执行查询操作
                int ser = sc.nextInt();    //要查询的值
                int start = sc.nextInt();   //起点
                int end = sc.nextInt();    //终点
                for (int j = (start-1);j <= (end-1) && j < data.size();j++){
                    if(ser == data.get(j).value){
                        res[count]+=1;  //第count个数组元素+1
                    }
                }
                count++;
            }
        }
        for (int i = 0; i < count;i++){
            System.out.println(res[i]);
        }
    }
}

class data{
    int id;
    int value;

    public data(int id, int value) {
        this.id = id;
        this.value = value;
    }
}
2024/9/17 18:30
加载中...