zoukankan      html  css  js  c++  java
  • 数据结构实验之排序七:选课名单 (SDUT 3404)

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct node
    {
        char data[15];
        struct node *next;  //存放名字
    };
    
    struct node *head[2018];  // 每个课程都有一个相应的开始
    int num[2018];  // 存放每个课程选的人数
    char name[15];
    
    int main()
    {
    
        int n,m,sum,cnt;
        while(~scanf("%d%d",&n,&m))
        {
            memset(num,0,sizeof(num));
            for(int i = 0; i < 2018; i ++)  // 初始化
            {
                head[i] = (struct node *)malloc(sizeof(struct node));
                head[i] -> next = NULL;
            }
            for(int i = 0; i < n; i ++)  
            {
                scanf("%s %d", name, &sum); 
                for(int j = 0; j < sum; j ++)
                {
                    scanf("%d", &cnt);  
                    num[cnt] ++;  // 对应的课程的人数要+1
                    struct node *q, *p;
                    q = (struct node *)malloc(sizeof(struct node));
                    q -> next = NULL;  
                    strcpy(q -> data,name); // 新申请一个结点来存放这个人的名字
                    p = head[cnt]; // 找到这个人应该的在的那一列中
                    while(p -> next)
                    {
                        if(strcmp(q -> data, p -> next -> data) < 0) // 找到第一个字符串需要大于它的
                        {
                            break;
                        }
                        p = p -> next;
                    }
                    q -> next = p -> next;  // 顺序插入到链表中去
                    p -> next = q;
                }
            }
            for(int i = 1; i <= m; i ++)
            {
                printf("%d %d
    ", i, num[i]); // 输出课程 以及人数
                struct node *p; // 对应的人名
                p = head[i] -> next;
                while(p)
                {
                    printf("%s
    ", p -> data);
                    p = p -> next;
                }
            }
    
        }
        return 0;
    }
    
  • 相关阅读:
    OA常见问题和解决方案
    如何用Visio画venn(维恩)图
    小谈SQL表的连接
    记一次视图的应用
    常用sql语句备份
    EF中关系映射问题
    .net core 2.0的一次奇特经历
    .net core 下的Area注册
    win 10+ iis 10 部署.net core 1.1 web api
    AutoMapper差异内容备份
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139396.html
Copyright © 2011-2022 走看看