Java—整理音乐
Description
请用链表完成下面题目要求。
xiaobai 很喜欢音乐,几年来一直在收集好听的专辑。他有个习惯,每次在听完一首音乐后会给这首音乐打分,而且会隔一段时间给打好分的音乐排一个名次。今天 xiaobai 打开自己的音乐文件夹,发现有很多不同时期打过分的排好序的子音乐文件夹,他想把这些音乐放到一块,组成一个分数有序的序列。由于音乐文件很多,而文件里音乐的数目也是不确定的,怎么帮帮 xiaobai 完成这件工作呢?
Input
输入数据第一行为一个整数n(n<1000),代表文件夹的数量。接下来是n个文件夹的信息,每个文件夹信息的第一行是一个数字m(m<=10000),代表这个文件夹里有m首歌,后面m行每行一个歌曲名、分数,之间用空格分开。歌曲名称不超过5个字符。
Output
输出一行,为所有音乐组成的一个序列,音乐只输出名字。
如果音乐分数相同则按照音乐名字典序进行排序。
Sample
Input
3
4
aaa 60
aab 50
aac 40
aad 30
2
kkk 60
kkd 59
3
qow 70
qwe 60
qqw 20
Output
qow aaa kkk qwe kkd aab aac aad qqw
import java.util.Scanner;
class Music {
String name;
int score;
public Music(String name, int score) {
this.name = name;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
Music music[] = new Music[100010];// 数组
int number = 0;
int n = reader.nextInt();
while (n-- > 0) {
int m = reader.nextInt();
reader.nextLine();// 吃掉回车
for (int i = 0; i < m; i++) {
String str = reader.nextLine();
String ch[] = str.split(" ");
String name = ch[0];
int score = Integer.parseInt(ch[1]);
music[number] = new Music(name, score);
number++;
}
}
// 排序(音乐分数由高到低,如果音乐分数相同则按照音乐名字典序进行排序)
for (int i = 0; i < number - 1; i++) {
for (int j = 0; j < number - 1 - i; j++) {
if (music[j].score < music[j + 1].score) {
Music temp = music[j];
music[j] = music[j + 1];
music[j + 1] = temp;
} else if (music[j].score == music[j + 1].score) {
if (music[j].name.compareTo(music[j + 1].name) > 0) {
Music temp = music[j];
music[j] = music[j + 1];
music[j + 1] = temp;
}
}
}
}
for (int i = 0; i < number; i++) {
System.out.print(music[i].name + " ");
}
}
}