整理音乐

整理音乐

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem 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

Sample Output

扫描二维码关注公众号,回复: 3187915 查看本文章
qow aaa kkk qwe kkd aab aac aad qqw

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    int num;
    char name[20];
    struct node *next;
};

struct node *kk(int a)
{
    int b;
    struct node *head,*p,*t;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    t=head;
    for(b=0;b<a;b++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%s %d",p->name,&p->num);
        p->next=NULL;
        t->next=p;
        t=p;
    }
    return head;
};

struct node *zz(struct node *head,struct node *head1)
{
    struct node *p,*q,*t;
    p=head->next;
    q=head1->next;
    head->next=NULL;
    head1->next=NULL;
    t=head1;
    while(p&&q)
    {
        if(p->num>q->num)
        {
            t->next=p;
            t=p;
            p=p->next;
            t->next=NULL;
        }
        else if(q->num>p->num)
        {
            t->next=q;
            t=q;
            q=q->next;
            t->next=NULL;
        }
        else if(q->num==p->num)
        {
            if(strcmp(p->name,q->name)<0)
            {
                t->next=p;
                t=p;
                p=p->next;
                t->next=NULL;
            }
            else
            {
                t->next=q;
                t=q;
                q=q->next;
                t->next=NULL;
            }
        }
    }
    if(p)
    {
        t->next=p;
    }
    else
    {
        t->next=q;
    }
    return head1;
};

int main()
{
    int a,b,f,h;
    struct node *head[1005];
    scanf("%d",&a);
    for(b=0;b<a;b++)
    {
        scanf("%d",&f);
        head[b]=kk(f);
    }
    for(h=a-1;h>=1;h--)
    {
        head[h-1]=zz(head[h],head[h-1]);
    }
    while(head[0]->next!=NULL)
    {
        head[0]=head[0]->next;
        printf("%s ",head[0]->name);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The_city_of_the__sky/article/details/81750264
今日推荐