PAT乙级 1095 解码PAT准考证 (16/25 分) Java 实现

笔者能力有限,有点难,测试点2.3.4超时,学习了小蔡不想敲代码的代码。

import java.io.*;
import java.util.*;

public class pat1095{
	
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String arr [] = br.readLine().split(" ");
        int a = Integer.valueOf(arr[0]);
        int b = Integer.parseInt(arr[1]);
        Student1095 [] info = new Student1095[a];
        for (int i = 0; i < a; i++) {
            info[i] = new Student1095(br.readLine());
        }
        Did1095 [] op = new Did1095[b];
        for (int i = 0; i < b; i++) {
            op[i] = new Did1095(br.readLine());
        }
        Arrays.sort(info);								//成绩降序,准考证升序排序
        for (int i = 0; i < b; i++){
            if(op[i].number==1){						//判断等级
                int temp = 0;
                System.out.println("Case " + (i + 1) + ": " + op[i].number + " " + op[i].did);
                for (int j = 0; j < a; j++) {
                    if(info[j].level.equals(op[i].did)){
                        if(temp==0){ temp++; }
                        System.out.println(info[j].toString()+" "+info[j].score);
                    }
                }
                if(temp==0) System.out.println("NA");
            }else if(op[i].number==2){					//考场编号
                int people = 0;
                int sum = 0;
                int temp = 0;
                System.out.println("Case "+(i+1)+": "+op[i].number+" "+op[i].did);
                for (int j = 0; j < a; j++) {
                    if (info[j].num1.equals(op[i].did)){
                        sum += info[j].score;
                        people++;
                        temp++;
                    }
                }
                if(temp==0) System.out.println("NA");
                else System.out.printf("%d %d\n", people, sum);
            }else if(op[i].number==3){
                List<Compare> list1 = new ArrayList<>();
                System.out.println("Case "+(i+1)+": "+op[i].number+" "+op[i].did);
                for (int j = 0; j < a; j++) {
                    String number1 = info[j].num1;
                    if(op[i].did.equals(info[j].date)){
                        boolean ok = false;
                        if(list1.size()!=0){
                            for (int k = 0; k < list1.size(); k++) {			//找list中重复的值
                                if (list1.get(k).number1.equals(number1)) {
                                    ok = true;
                                    list1.get(k).sum++;							//sum++
                                    break;
                                }
                            }
                        }
                        if(!ok) list1.add(new Compare(info[j].num1, 1));
                    }
                }
                if(list1.size() == 0) System.out.println("NA");			//list为空
                else{
                    Collections.sort(list1);							//人数降序,考场号升序
                    for(Compare com: list1) System.out.println(com);
                }
            }
        }
    }
}
class Student1095 implements Comparable<Student1095>{
	
    public int score;										//成绩
    public String level;									//等级
    public String num1;										//考场编号
    public String date;										//日期
    public String num2;										//考生编号
    
    public Student1095(String string){						//只输入一个 String 然后分开
        this.level = string.substring(0,1);
        this.num1 = string.substring(1,4);
        this.date = string.substring(4,10);
        this.num2 = string.substring(10,13);
        this.score = Integer.parseInt(string.substring(14));
    }
    
    public int compareTo(Student1095 info){					//成绩降序,准考证升序
        if(this.score>info.score) return -1;
        else if(this.score<info.score) return 1;
        else return this.toString().compareTo(info.toString());
    }

    @Override
    public String toString() {
        return this.level+this.num1+this.date+this.num2;
    }
}

class Did1095{
	
    public int number;
    public String did;
    
    Did1095(String string){
        String []s = string.split(" ");
        this.number = Integer.valueOf(s[0]);
        this.did = s[1];
    }
}

class Compare implements Comparable<Compare>{
	
    public String number1;
    public int sum;

    public Compare(String number1, int sum) {
        this.number1 = number1;
        this.sum = sum;
    }

    @Override
    public int compareTo( Compare o) {						//人数降序,考场升序
        if(this.sum > o.sum) return -1;
        else if(this.sum < o.sum) return 1;
        else return this.number1.compareTo(o.number1);
    }

    @Override
    public String toString() {
        return this.number1+" "+this.sum;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值