zoukankan      html  css  js  c++  java
  • 7-47 打印选课学生名单 (25分)

     

     

    解题思路:采用桶排序思想,将每个课程看成一个桶,再把每个课程对应学生放入桶,其中学生名单的采用二叉排序树结构存储

    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    
    #define MaxSize 2503
    #define MaxS 4
    typedef char Element[MaxS+1];
    
    typedef struct TNode *Tree;
    struct TNode{
        Element Name;
        struct TNode *Left,*Right;
    };
    
    typedef struct HashTbl *HashTable;
    struct HashTbl{
        int Num;
        Tree Next;
    };
    HashTable InitialHashTable(int Size);
    void Insert(HashTable H,int CourseNo,Element Name);
    Tree BuildBiSearchTree(Tree T,Element Name);
    void Trav(Tree T);
    void Out(HashTable H,int M);
    
    int main()
    {
        int N,M,i,j,n,CourseNo;
        Element Name;
        HashTable H;
        
        scanf("%d %d",&N,&M);
        H=InitialHashTable(M+1);
        
        for(i=0;i<N;i++)
        {
            scanf("%s%d",&Name,&n);
            
            for(j=0;j<n;j++)
            {
                scanf("%d",&CourseNo);
                Insert(H,CourseNo,Name);
            }
        }
        
        Out(H,M);
        return 0;
    }
    
    
    HashTable InitialHashTable(int Size)
    {
        HashTable H=malloc(sizeof(struct HashTbl)*Size);
        while(Size)
        {
            H[--Size].Next=NULL;
            H[Size].Num=0;
        }
        return H;
    }
    
    void Insert(HashTable H,int CourseNo,Element Name)
    {
        Tree T=H[CourseNo].Next;
        H[CourseNo].Num++;
        H[CourseNo].Next=BuildBiSearchTree(T,Name);
    }
    
    Tree BuildBiSearchTree(Tree T,Element Name)
    {
        if(!T)
        {
            T=malloc(sizeof(struct TNode));
            strcpy(T->Name,Name);
            T->Left=NULL;
            T->Right=NULL;
        }
        else if(strcmp(Name,T->Name)<0)
        {
            T->Left=BuildBiSearchTree(T->Left,Name);
        }
        else
        {
            T->Right=BuildBiSearchTree(T->Right,Name);
        }
        return T;
    }
    
    void Trav(Tree T)
    {
        if(T)
        {
            Trav(T->Left);
            printf("%s
    ",T->Name);
            Trav(T->Right);
        }
    }
    void Out(HashTable H,int M)
    {
        int i;
        for(i=1;i<=M;i++)
        {
            printf("%d %d
    ",i,H[i].Num);
            Tree T=H[i].Next;
            Trav(T);
        }
    }

  • 相关阅读:
    苹果p12文件--一个苹果证书怎么多次使用(蛋疼,这些问题只有和其他企业合作才会遇到,别人的账号不可能给你,蛋疼....)
    xcode 树形管理 cocos2d-x的资源
    cocos2d-x 扩充引擎基类功能 引起的头文件重复包含问题的分析
    pytest--运行指定的测试和参数化
    adb--常用命令
    appium--desktop
    adb--环境安装
    pytest--命令行常用参数
    django -- auth模块
    pytest--常用插件
  • 原文地址:https://www.cnblogs.com/snzhong/p/12662844.html
Copyright © 2011-2022 走看看