SDUT 2053 整理音乐

此题还是上机考试题,也算是水题吧。注意要求是用链表做。

注意换行getchar()。然后就是字典序排列用字符串函数。排序用链表的归并。

归并后将它给一个指针存起来。

题目描述


请用链表完成下面题目要求。
xiaobai 很喜欢音乐,几年来一直在收集好听的专辑。他有个习惯,每次在听完一首音乐后会给这首音乐打分,而且会隔一段时间给打好分的音乐排一个名次。今天 xiaobai 打开自己的音乐文件夹,发现有很多不同时期打过分的排好序的子音乐文件夹,他想把这些音乐放到一块,组成一个分数有序的序列。由于音乐文件很多,而文件里音乐的数目也是不确定的,怎么帮帮 xiaobai 完成这件工作呢?
   

输入

输入数据第一行为一个整数n(n<1000),代表文件夹的数量。接下来是n个文件夹的信息,每个文件夹信息的第一行是一个数字m,代表这个文件夹里有m首歌,后面m行每行一个歌曲名、分数,之间用空格分开。

输出

输出一行,为所有音乐组成的一个序列,音乐只输出名字。

如果音乐分数相同则按照音乐名字典序进行排序。

示例输入

3
4
aaa 60
aab 50
aac 40
aad 30
2
kkk 60
kkd 59
3
qow 70
qwe 60
qqw 20

示例输出

qow aaa kkk qwe kkd aab aac aad qqw

 

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 struct node
 5 {
 6     int data;
 7     char name [200];
 8     struct node*next;
 9 };
10 struct node*creat(int n)
11 {
12     struct node*head,*p,*tail;
13     int i;
14     head=(struct node*)malloc(sizeof(struct node));
15     head->next=NULL;
16     tail=head;
17     for(i=1;i<=n;i++)
18     {
19         p=(struct node*)malloc(sizeof(struct node));
20         scanf("%s",&p->name);
21         scanf("%d",&p->data);
22         p->next=NULL;
23         tail->next=p;
24         tail=p;
25     }
26     return head;
27 
28 }
29 void list(struct node*r)
30 {
31     struct node*m;
32     m=r;
33     while(m->next->next!=NULL)
34     {
35         printf("%s ",m->next->name);
36         m=m->next;
37     }
38     printf("%s\n",m->next->name);
39 }
40 struct node*merge(struct node*head1,struct node*head2)
41 {
42     struct node *p1,*p2,*tail;
43     p1=head1->next;
44     p2=head2->next;
45     tail=head1;
46     free (head2);
47     while(p1&&p2)
48 
49         if(p1->data<p2->data)
50         {
51             tail->next=p2;
52             tail=p2;
53             p2=p2->next;
54         }
55         else if(p1->data==p2->data&&strcmp(p1->name,p2->name)>0)
56         {
57             tail->next=p2;
58             tail=p2;
59             p2=p2->next;
60         }
61         else
62         {
63             tail->next=p1;
64             tail=p1;
65             p1=p1->next;
66         }
67         if(p1)
68         tail->next=p1;
69         else
70         tail->next=p2;
71         return head1;
72 }
73 int main()
74 {
75     int n,m;
76     scanf("%d",&m);
77     struct node*head1,*head2,*l;
78     scanf("%d",&n);
79     getchar();
80     head1=creat(n);
81     m--;
82     while(m--)
83     {
84         scanf("%d",&n);
85         getchar();
86         head2=creat(n);
87         l=merge(head1,head2);
88     }
89     list(l);
90     return 0;
91 }

 

posted @ 2012-05-27 09:18  时光旅行的懒猫  阅读(229)  评论(0编辑  收藏  举报