POJ 2418 Hardwood Species (二叉查找树--插入操作)

左手的ㄟ右手 2024-02-17 21:19 61阅读 0赞

Hardwood Species














Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24658   Accepted: 9530

Description

Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter.
America’s temperate climates produce forests with hundreds of hardwood species — trees that share certain biological characteristics. Although oak, maple and cherry all are types of hardwood trees, for example, they are different species. Together, all the hardwood species represent 40 percent of the trees in the United States.

On the other hand, softwoods, or conifers, from the Latin word meaning “cone-bearing,” have needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods are used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative applications.

Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particular day. You are to compute the total fraction of the tree population represented by each species.

Input

Input to your program consists of a list of the species of every tree observed by the satellite; one tree per line. No species name exceeds 30 characters. There are no more than 10,000 species and no more than 1,000,000 trees.

Output

Print the name of each species represented in the population, in alphabetical order, followed by the percentage of the population it represents, to 4 decimal places.

Sample Input

  1. Red Alder
  2. Ash
  3. Aspen
  4. Basswood
  5. Ash
  6. Beech
  7. Yellow Birch
  8. Ash
  9. Cherry
  10. Cottonwood
  11. Ash
  12. Cypress
  13. Red Elm
  14. Gum
  15. Hackberry
  16. White Oak
  17. Hickory
  18. Pecan
  19. Hard Maple
  20. White Oak
  21. Soft Maple
  22. Red Oak
  23. Red Oak
  24. White Oak
  25. Poplan
  26. Sassafras
  27. Sycamore
  28. Black Walnut
  29. Willow

Sample Output

  1. Ash 13.7931
  2. Aspen 3.4483
  3. Basswood 3.4483
  4. Beech 3.4483
  5. Black Walnut 3.4483
  6. Cherry 3.4483
  7. Cottonwood 3.4483
  8. Cypress 3.4483
  9. Gum 3.4483
  10. Hackberry 3.4483
  11. Hard Maple 3.4483
  12. Hickory 3.4483
  13. Pecan 3.4483
  14. Poplan 3.4483
  15. Red Alder 3.4483
  16. Red Elm 3.4483
  17. Red Oak 6.8966
  18. Sassafras 3.4483
  19. Soft Maple 3.4483
  20. Sycamore 3.4483
  21. White Oak 10.3448
  22. Willow 3.4483
  23. Yellow Birch 3.4483

题意:给出一些树,求每种树在总数中的比例

分析:本题为了练习二叉查找树,故用BST求解,当然只用到了其插入操作。

AC代码:

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. using namespace std;
  5. const int maxn=1e4+10;
  6. typedef struct node{
  7. char data[32];
  8. int cnt;
  9. node* lchild,*rchild;
  10. } *Tree,tree;
  11. Tree root;
  12. int n;
  13. void jisuan(Tree root){
  14. if(root!=NULL){
  15. jisuan(root->lchild);
  16. printf("%s %.4lf\n",root->data,(double)(root->cnt)/n*100);
  17. jisuan(root->rchild);
  18. }
  19. }
  20. void InsertBST(Tree& root,char *data){
  21. if(root==NULL){
  22. root=(Tree)malloc(sizeof(tree));
  23. strcpy(root->data,data);
  24. root->lchild=NULL; root->rchild=NULL;
  25. root->cnt=1;
  26. return ;
  27. }
  28. if(strcmp(data,root->data)==0){
  29. root->cnt++;
  30. return ;
  31. }
  32. if(strcmp(data,root->data)>0){
  33. InsertBST(root->rchild,data);
  34. return ;
  35. }
  36. if(strcmp(data,root->data)<0){
  37. InsertBST(root->lchild,data);
  38. return ;
  39. }
  40. }
  41. int main(){
  42. char s[32];
  43. n=0;
  44. while(gets(s)){
  45. InsertBST(root,s);
  46. n++;
  47. }
  48. jisuan(root);
  49. return 0;
  50. }

发表评论

表情:
评论列表 (有 0 条评论,61人围观)

还没有评论,来说两句吧...

相关阅读