閱讀807 返回首頁    go 阿裏雲 go 技術社區[雲棲]


poj2418 Hardwood Species 排序二叉樹

很基本的排序二叉樹

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int all;
struct node
{
    bool used;
    char name[35];
    int num,l,r;
};
node tree[10005];
char s[35];
int cnt;
void increase(int now)
{
    node &nn=tree[now];
    if(nn.used==0)
    {
        strcpy(nn.name,s);
        nn.used=1;
        nn.num++;
        return;
    }
    int c=strcmp(s,nn.name);
    if(c<0)
    {
        if(nn.l==0)nn.l=++cnt;
        increase(nn.l);
    }
    else if(c>0)
    {
        if(nn.r==0)nn.r=++cnt;
        increase(nn.r);
    }
    else nn.num++;
}
void output(int now)
{
    node &nn=tree[now];
    if(nn.used==0)return;
    output(nn.l);
    printf("%s %.4f\n",nn.name,(double)nn.num/all*100.0);
    output(nn.r);
    return;
}
int main()
{
    memset(tree,0,sizeof(tree));
    all=0;cnt=1;
    while(gets(s))
    {
        increase(1);
        all++;
    }
    output(1);
}


 

最後更新:2017-04-04 07:03:20

  上一篇:go linux下使用modprobe命令
  下一篇:go 蘋果戰世界:蘋果史上27大口水戰