zoukankan      html  css  js  c++  java
  • SDUT-3375_数据结构实验之查找三:树的种类统计

    数据结构实验之查找三:树的种类统计

    Time Limit: 400 ms Memory Limit: 65536 KiB

    Problem Description

    随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。

    Input

    输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。

    Output

    按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。

    Sample Input

    2
    This is an Appletree
    this is an appletree

    Sample Output

    this is an appletree 100.00%

    题解:数据量比较大,需要用到字典树。
    字典树跟排序树类似,根据字符串的字典序进行的排序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char s[25];
    
    typedef struct node
    {
        char s[25];
        int num;
        struct node *l,*r;
    }node;
    
    node *newNode()
    {
        node *t;
        t = (node *)malloc(sizeof(node));
        t->l = t->r = NULL;
        return t;
    }
    
    node * build(node *t)
    {
        if(t!=NULL)
        {
            if(strcmp(t->s,s)==0)
                t->num ++;
            else if(strcmp(t->s,s)<0)
                t->r = build(t->r);
            else if(strcmp(t->s,s)>0)
                t->l = build(t->l);
            return t;
        }
        t = newNode();
        strcpy(t->s,s);
        t->l = NULL;
        t->r = NULL;
        t->num = 1;
        return t;
    }
    
    void show(node *t,int n)
    {
        if(t)
        {
            show(t->l,n);
            printf("%s ",t->s);
            printf("%.2f%%
    ",1.0*t->num/n*100);
            show(t->r,n);
        }
    }
    
    void del(node *t)
    {
        if(t)
        {
            del(t->r);
            del(t->l);
            free(t);
        }
    }
    
    int main()
    {
        int n,i,j;
        node *t;
        scanf("%d",&n);
        t = NULL;
        getchar();
        for(i=0;i<n;i++)
        {
            gets(s);
            int len = strlen(s);
            for(j=0;j<len;j++)
                if(s[j]>='A'&&s[j]<='Z')
                    s[j] = s[j] - 'A' + 'a';
            //cout<<s<<endl;
            t = build(t);
            //printf("%d
    ",t->num);
        }
        show(t,n);
        del(t);
        return 0;
    }
    
    
  • 相关阅读:
    HAproxy 1.5 dev14 发布
    IBM/DW 使用 Java 测试网络连通性的几种方法
    Skype 4.1 Linux 发布,支持微软帐号登录
    Dorado 7.1.20 发布,Ajax的Web开发平台
    Aspose.Slides for Java 3.0 发布
    开发版本 Wine 1.5.18 发布
    BitNami Rubystack 开始支持 Ruby 2.0
    XWiki 4.3 正式版发布
    Silverlight实例教程 Out of Browser的Debug和Notifications窗口
    Silverlight实例教程 Out of Browser与Office的互操作
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/10136037.html
Copyright © 2011-2022 走看看