zoukankan      html  css  js  c++  java
  • 5-24 树种统计 (25分)

                                                    5-24 树种统计   (25分)

    随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。

    输入格式:

    输入首先给出正整数N(le 10^5105​​),随后N行,每行给出卫星观测到的一棵树的种类名称。种类名称由不超过30个英文字母和空格组成(大小写不区分)。

    输出格式:

    按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位。

    输入样例:

    29
    Red Alder
    Ash
    Aspen
    Basswood
    Ash
    Beech
    Yellow Birch
    Ash
    Cherry
    Cottonwood
    Ash
    Cypress
    Red Elm
    Gum
    Hackberry
    White Oak
    Hickory
    Pecan
    Hard Maple
    White Oak
    Soft Maple
    Red Oak
    Red Oak
    White Oak
    Poplan
    Sassafras
    Sycamore
    Black Walnut
    Willow
    

    输出样例:

    Ash 13.7931%
    Aspen 3.4483%
    Basswood 3.4483%
    Beech 3.4483%
    Black Walnut 3.4483%
    Cherry 3.4483%
    Cottonwood 3.4483%
    Cypress 3.4483%
    Gum 3.4483%
    Hackberry 3.4483%
    Hard Maple 3.4483%
    Hickory 3.4483%
    Pecan 3.4483%
    Poplan 3.4483%
    Red Alder 3.4483%
    Red Elm 3.4483%
    Red Oak 6.8966%
    Sassafras 3.4483%
    Soft Maple 3.4483%
    Sycamore 3.4483%
    White Oak 10.3448%
    Willow 3.4483%
    Yellow Birch 3.4483%
    按字典序排列的问题可以多考虑用二叉树

    方法一代码:(用STL编写)
     1 #include <iostream>  
     2 #include <string>  
     3 #include <map>  
     4 #include <algorithm>  
     5   
     6 #define MAX 100000 + 10  
     7   
     8 using namespace std;  
     9   
    10 typedef struct {  
    11     string name;  
    12     int num;  
    13 } Tree;  
    14   
    15 Tree tree[MAX];  
    16   
    17 int member = 1;  
    18 map<string, int> mp;  
    19   
    20 void findTree( string name ) {  
    21     if( mp[name] == 0 ) {  
    22         mp[name] = member;  
    23   
    24         tree[member].name = name;  
    25         tree[member].num++;  
    26   
    27         member++;  
    28     }  
    29     else {  
    30         int id = mp[name];  
    31         tree[id].num++;  
    32     }  
    33   
    34 }  
    35   
    36 int cmp( Tree a, Tree b ) {  
    37     return a.name < b.name ? 1 : 0;  
    38 }  
    39   
    40 int main() {  
    41     //freopen( "123.txt", "r", stdin );  
    42     int n;  
    43     scanf( "%d", &n );  
    44     char ch = getchar();  
    45     string name;  
    46     for( int i = 0; i < n; i++ ) {  
    47         getline( cin, name );  
    48         findTree( name );  
    49     }  
    50   
    51     sort( tree + 1, tree + member, cmp );  
    52   
    53     for( int i = 1; i < member; i++ ) {  
    54         printf( "%s %.4lf%%
    ", tree[i].name.c_str(), (double)tree[i].num * 100.0 / n );  
    55     }  
    56   
    57     return 0;  
    58 }  
    
    

    方法二代码:(二叉搜索树)

    
    
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #define MAX 30
     5 typedef char ElemType;
     6 typedef struct node *Bintree;
     7 typedef struct node
     8 {
     9     ElemType s[MAX+1];
    10     int count;
    11     Bintree lchild;
    12     Bintree rchild;
    13 }node;
    14 Bintree Insert(Bintree BST,ElemType s[])
    15 {
    16     if(!BST)
    17     {
    18         BST=(Bintree)malloc(sizeof(struct node));
    19         strcpy(BST->s,s);
    20         BST->count=1;
    21         BST->lchild=BST->rchild=NULL;
    22     }
    23     else
    24         if(strcmp(BST->s,s)<0)
    25             BST->rchild=Insert(BST->rchild,s);
    26         else if(strcmp(BST->s,s)>0)
    27             BST->lchild=Insert(BST->lchild,s);
    28         else
    29             BST->count++;
    30     return BST;
    31 }
    32 void Inorder(Bintree BST,int N)//中序遍历搜索二叉搜索树,就得到了按字典序列递增的输出序列
    33 {
    34     if(BST)
    35     {
    36         Inorder(BST->lchild,N);
    37         printf("%s %.4f
    ",BST->s,(float)BST->count/N*100);
    38         Inorder(BST->rchild,N);
    39     }
    40 }
    41 int main()
    42 {
    43     int N,i;
    44     char s[MAX];
    45     Bintree T;
    46     T=NULL;
    47     scanf("%d",&N);
    48     getchar();
    49     for(i=0;i<N;i++)
    50     {
    51         fgets(s,31,stdin);//用gets在很多编译器里面显示不安全,scanf又不能吸收中间的空格,所以fgets用用也是比较好的
    52         T=Insert(T,s);
    53     }
    54     Inorder(T,N);
    55     return 0;
    56 }
    
    
    
     
  • 相关阅读:
    mysql query insert中文乱码
    git rebase
    ubuntu下怎么合并windows下分割的zip包
    [Matlab]双线性变换法设计数字带阻滤波器
    [Matlab]双线性变换法设计数字带通滤波器
    [Matlab]双线性变换法设计数字高通滤波器
    [Matlab]双线性变换法设计数字低通滤波器
    [Matlab]四种IIR滤波器纹波特性对比
    [Matlab]椭圆滤波器设计:低通、高通、带通和带阻
    [Matlab]切比雪夫Ⅱ型滤波器设计:低通、高通、带通和带阻
  • 原文地址:https://www.cnblogs.com/guohaoyu110/p/6383921.html
Copyright © 2011-2022 走看看