poj 2418 Hardwood Species 排序二叉树

题意:有n个单词,要求每个单词出现的频率(次数/n),并把这些单词以字典序输出

View Code
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

struct Tree
{
int number;
char name[35];
Tree *l,*r;
Tree()
{
l=r=0;
}
}*root;

int n;
char s[35];

void init(Tree *&t,char *s)
{
if(t==NULL)
{
t=new Tree;
strcpy(t->name,s);
t->number=1;
return;
}
else
{
if(strcmp(t->name,s)==0)
{
t->number++;return;
}
else if(strcmp(t->name,s)<0)init(t->r,s);
else init(t->l,s);
}
}

void ino(Tree *t)
{
if(t)
{
ino(t->l);
printf("%s %.4f\n",t->name,(float)t->number*100/n);
ino(t->r);
}
}

int main()
{
n=0;
while (gets(s))
{
n++;
init(root,s);
}
ino(root);
return 0;
}



posted @ 2011-11-23 20:28  104_gogo  阅读(144)  评论(0编辑  收藏  举报