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说我内存用的过多否则也是可以满分的。欢迎大家评论,改进我的代码,可以自己尝试的去减少内存试试看能不能满分哇。


被折叠的 条评论
为什么被折叠?



