Problem Description
隨着衛星成像技術的應用,自然資源研究機構可以識別每一個棵樹的種類。請編寫程序幫助研究人員統計每種樹的數量,計算每種樹佔總數的百分比。
Input
輸入一組測試數據。數據的第1行給出一個正整數N (n <= 100000),N表示樹的數量;隨後N行,每行給出衛星觀測到的一棵樹的種類名稱,樹的名稱是一個不超過20個字符的字符串,字符串由英文字母和空格組成,不區分大小寫。
Output
按字典序輸出各種樹的種類名稱和它佔的百分比,中間以空格間隔,小數點後保留兩位小數。
Sample Input
2
This is an Appletree
this is an appletree
Sample Output
this is an appletree 100.00%
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int num;
char data[35];
struct node *left, *right;
};
struct node *add(struct node *root, char a[])
{
if(root==NULL)
{
root = (struct node *)malloc(sizeof(struct node));
root->left = NULL;
root->right = NULL;
strcpy(root->data, a);
root->num = 1;
}
else
{
if(strcmp(root->data, a) == 0) root->num++;
else if(strcmp(a, root->data) < 0) root->left = add(root->left, a);
else root->right = add(root->right, a);
}
return root;
}
void PRINT(struct node *root, int n)
{
if(root==NULL) return;
else
{
PRINT(root->left, n);
printf("%s %.2lf%%\n", root->data, root->num * 100.0 / n);
PRINT(root->right, n);
}
}
int main()
{
int N, n, i, len;
struct node *root;
char a[35];
scanf("%d", &n);
root = NULL;
getchar();
N = n;
while(n--)
{
gets(a);
len = strlen(a);
for(i=0; i<len; i++)
{
if(a[i]>='A' && a[i]<='Z') a[i] = a[i] + 'a' - 'A';
}
root = add(root, a);
}
PRINT(root, N);
return 0;
}