PTA打印选课学生名单 Java

1.题目:

 2.主要思想:在输入的时候就使用了了一个字符串数组,和一个整形的二维数组,后面运用了ArraList这个集合来存储名字,并自己采用字符串冒泡的方法来达到题目要求的名字按字典的字母输出的格式。

3.代码实现:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<>();
        int n = in.nextInt();
        int k = in.nextInt();
        String[] stu = new String[n];
        int[][] a = new int[n][k];
        for (int i = 0; i < n; i++) {
            stu[i] = in.next();
            int f = in.nextInt();
            for (int j = 0; j < f; j++) {
                a[i][j] = in.nextInt();
            }
        }
        int count = 0;
        for (int i = 1; i <= k; i++) {
            for (int i1 = 0; i1 < n; i1++) {
                for (int j1 = 0; j1 < k; j1++) {
                    if (a[i1][j1] == i) {
                        list.add(stu[i1]);
                        count++;
                    }
                }
            }
            System.out.println(i + " " + count);
            String[] str = new String[list.size()];
            for (int i1 = 0; i1 < str.length; i1++) {
                str[i1] = list.get(i1);
            }
            for (int i1 = 0; i1 < str.length - 1; i1++) {
                for (int j = 0; j < str.length - 1 - i1; j++) {
                    String temp;
                    if (str[j].compareTo(str[j + 1]) > 0) {
                        temp = str[j];
                        str[j] = str[j + 1];
                        str[j + 1] = temp;
                    }
                }
            }
            for (int i1 = 0; i1 < str.length; i1++) {
                System.out.println(str[i1]);
            }
            count = 0;
            list.clear();

        }
    }
}


4.运行结果:

 5.总结:非常遗憾在pta说我内存用的过多否则也是可以满分的。欢迎大家评论,改进我的代码,可以自己尝试的去减少内存试试看能不能满分哇。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值