POJ 2418 Hardwood Species

84 篇文章 0 订阅
4 篇文章 0 订阅

这个题可以用多种方法解决。

题目大意:

就是输入一堆字符串,可能重复,按字典序输出,并输出占的比例。

可以用字典树,二叉查找树,Map,快排等等多种方法。

我用的二叉查找树,下面是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
    int cut;
    char c[40];
    struct node *l,*r;
} head;
char s[40];
double f,cut;
void build()
{
    struct node *p,*q;
    p=&head;
    while(1)
    {
        if(strcmp(p->c,s)>0)
        {
            if(p->r==NULL)
            {
                q=(struct node *)malloc(sizeof(struct node));
                strcpy(q->c,s);
                q->cut=1;
                q->l=NULL;
                q->r=NULL;
                p->r=q;
                return ;
            }
            p=p->r;
        }
        else if(strcmp(p->c,s)==0)
        {
            p->cut++;
            return ;
        }
        else
        {
            if(p->l==NULL)
            {
                q=(struct node *)malloc(sizeof(struct node));
                strcpy(q->c,s);
                q->cut=1;
                q->l=NULL;
                q->r=NULL;
                p->l=q;
                return ;
            }
            p=p->l;
        }
    }
}
void pr(struct node *p)
{
    if(p==NULL)
    {
        return;
    }
    pr(p->r);
    f=p->cut;
    printf("%s %.4f\n",p->c,(f*100.00)/cut);
    pr(p->l);
}
int main()
{
    int flat=1;
    cut=0;
    while(gets(s))
    {
        if(flat)
        {
            strcpy(head.c,s);
            head.cut=1;
            head.l=NULL;
            head.r=NULL;
            flat=0;
        }
        else
        {
            build();
        }
        cut++;
    }
    pr(&head);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值