7-24 树种统计 (25 分)

7-24 树种统计 (25 分)

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

输入格式:

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

输出格式:

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

输入样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

输出样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode
{
char data[35];
BinTree left;
BinTree right;
int count;
};
BinTree Insert( BinTree BT , char name[]);
void Print( BinTree BT , int n);
int main()
{
int n;
int i;
BinTree BT = NULL;

scanf("%d",&n);
getchar();
for( i=0; i<n; i++){
char name[35];
gets(name);
BT = Insert( BT,name);
}
Print( BT ,n);

return 0;
}


BinTree Insert( BinTree BT , char name[])
{
if( !BT ){
BT = ( BinTree )malloc(sizeof(struct TNode));
strcpy(BT->data,name);
BT->count=1;
BT->left = BT->right =NULL;
}
else{
int a = strcmp( BT->data,name);
if( a<0 ) BT->right = Insert( BT->right,name);

else if( a>0 ) BT->left=Insert( BT->left,name);
else BT->count++;
}
return BT;
}

void Print( BinTree BT , int n)
{
if( !BT ) return;
else{
Print( BT->left,n);
printf("%s ",BT->data);
printf("%.4f%%\n",(double)BT->count/n*100.0);
Print( BT->right,n);
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×