• poj 2418 Hardwood Species


    二叉排序树,第一次做的时候状态不好,就放弃了,现在一看,怎么这么简单?

     1 #include <stdio.h>
     2 #include <string.h>
     3 struct Node
     4 {
     5     char name[40];
     6     int d,lc,rc;
     7     void init(char *t)
     8     {
     9         strcpy(name,t);
    10         lc = rc = 0;
    11         d = 1;
    12     }
    13 }a[10050];
    14 int cnt = 1,num=0;
    15 void inst(char *s, int n)
    16 {
    17     Node &u = a[n];
    18     int f = strcmp(s,u.name);
    19     if(!f) u.d++;
    20     else if(f < 0)
    21         if(u.lc) inst(s,u.lc);
    22         else u.lc = ++num,a[num].init(s);
    23     else
    24         if(u.rc) inst(s,u.rc);
    25         else u.rc = ++num,a[num].init(s);
    26 }
    27 void show(int n)
    28 {
    29     Node &u = a[n];
    30     if(u.lc) show(u.lc);
    31     double t = double(u.d)/ cnt*100;
    32     printf("%s %.4lf\n",u.name,t);
    33     if(u.rc) show(u.rc);
    34 }
    35 int main()
    36 {
    37     char t[40];
    38     gets(t);
    39     a[0].init(t);
    40     while(gets(t)!=NULL)
    41         cnt++,inst(t,0);
    42     show(0);
    43     return 0;
    44 }
  • 相关阅读:
    【PAT甲级】1054 The Dominant Color
    【PAT甲级】1001 A+B Format
    【算法】二分排序和二分查找
    【PAT甲级】1008 Elevator (20分)
    移动端工作准备
    媒体查询
    多列布局
    怪异盒模型
    弹性盒
    圆角
  • 原文地址:https://www.cnblogs.com/lzxskjo/p/2647817.html
Copyright © 2020-2023  润新知