2018/12/04 PAT刷题 L1-005 java [ArrayList容器的使用, 在容器中添加类对象]

 

部分正确答案:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum=sc.nextInt();
        String[][] str = new String[sum][3];
        for (int i=0; i<sum; i++) {
            for (int j=0; j<3; j++) {
                str[i][j] = sc.next();
            }
        }
        int testSum = sc.nextInt();
        String[] str2 = new String[testSum];
        for (int i=0; i<testSum; i++) {
            str2[i]=sc.next();
        }
        for(int i=0; i<testSum; i++) {
            for(int j=0; j<sum; j++) {
                if(str2[i].equals(str[j][1])) {
                    System.out.println(str[j][0]+" "+str[j][2]);
                }
            }
        }
    }
}

 

使用BufferedReader优化了任然无法通过全部的样例

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
//import java.util.List;

public class Main {
    static class Stu {
        String id;
        int test;
        int seat;

        public Stu(String id, int test, int seat) {
            this.id = id;
            this.test = test;
            this.seat = seat;
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        ArrayList<Stu> list = new ArrayList<Stu>();
        for (int i = 0; i < n; i++) {
            String[] in = br.readLine().split(" ");
            Stu s = new Stu(in[0], Integer.parseInt(in[1]), Integer.parseInt(in[2]));
            list.add(s);
        }
        int tn = Integer.parseInt(br.readLine());
        String[] search = br.readLine().split(" ");
        br.close();
        for (int i = 0; i < search.length; i++) {
            for (Stu a : list) {
                if (a.test == Integer.parseInt(search[i])) {
                    System.out.println(a.id + " " + a.seat);
                }
            }
        }
    }
}

 

posted @ 2018-12-04 09:53  HHZZHH  阅读(299)  评论(1编辑  收藏  举报