整理音乐(SDUT--2053)

博客围绕用链表完成音乐整理排序展开。xiaobai有多个已排序的子音乐文件夹,需将其合并成一个分数有序序列。输入包含文件夹数量及各文件夹内歌曲信息,输出为按分数和字典序排序的歌曲名序列,还给出了排序思路及代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整理音乐

Time Limit: 1000 ms     Memory Limit: 65536 KiB

Submit   Statistic   Discuss

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

qow aaa kkk qwe kkd aab aac aad qqw

Hint:一遍分数排序,一遍相同分数字典序排序。本来想着一起判断,但总是出错,就拿出来分开判断。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    int data;
    char name[10];
    struct node *next;
} *head, *tail1, *tail2, *p, *q, *t;
int main()
{
    int n, m;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    tail1 = head;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &m);
        while(m--)  // 顺序建表
        {
            p = (struct node *)malloc(sizeof(struct node));
            p->next = NULL;
            scanf("%s %d", p->name, &p->data);
            tail1->next = p;
            tail1 = p;
        }
    }
    p = head;
    while(p->next) // 分数从大到小排序
    {
        tail1 = p;
        q = p->next;
        tail2 = q;
        t = q->next;
        while(t)
        {
            if(t->data > q->data)
            {
                tail2->next = t->next;
                t->next = tail1->next;
                tail1->next = t;
                t = tail2->next;
            }
            else
            {
                tail2 = t;
                t = t->next;
            }
        }
        p = p->next;
    }
    p = head;
    while(p->next)
    {
        tail1 = p;
        q = p->next;
        tail2 = q;
        t = q->next;
        while(t)
        {
            if((t->data == q->data && strcmp(t->name, q->name) < 0))//将相同分数字典序排序
            {
                tail2->next = t->next;
                t->next = tail1->next;
                tail1->next = t;
                t = tail2->next;
            }
            else
            {
                tail2 = t;
                t = t->next;
            }
        }
        p = p->next;
    }
    p = head->next;
    while(p)
    {
        printf("%s%c", p->name, p->next?' ':'\n');
        p = p->next;
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMYBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值