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);
}


 

目录
相关文章
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
55 0
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
|
人工智能 网络架构
|
人工智能 C++
poj-1012-约瑟夫问题
Description The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, .
814 0

热门文章

最新文章