整理音乐_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
Hint
import java.util.Scanner;class Mu{String name;int x;public Mu(String name, int x) {this.name = name;this.x = x;} }public class Main {public static void main(String[] args) {Scanner reader = new Scanner(System.in);Mu[] t=new Mu[11000];int num = 0;int n = reader.nextInt();while(n-- > 0) {int m = reader.nextInt();while(m-- > 0) {String name = reader.next();int x = reader.nextInt();t[num++] = new Mu(name, x);}}for(int i = 0; i < num - 1; i++) {for(int j = 0; j < num - 1 - i; j++) {if(t[j].x < t[j + 1].x || (t[j].x == t[j + 1].x && t[j].name.compareTo(t[j + 1].name) > 0)) {Mu tt = t[j];t[j] = t[j + 1];t[j + 1] = tt;}}}for(int i = 0; i < num; i++) {if(i == 0)System.out.print(t[i].name);elseSystem.out.print(" " + t[i].name);}reader.close();} }總結
- 上一篇: 数据结构实验之二叉树二:遍历二叉树
- 下一篇: 计算长方形的周长和面积(类和对象)_JA