Time Limit: 400MS
Memory Limit: 65536KB
Submit
Statistic
Discuss
Problem Description
随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。
Input
输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。
Output
按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。
Example Input
2
This is an Appletree
this is an appletree
Example Output
this is an appletree 100.00%
Hint
Author
xam
可以用二叉排序树做,结构体加入一个变量num记录某条语句的出现次数,
将输入的字符串统一转为小写,最后中序遍历二叉树输出字符串
#include <bits/stdc++.h>
using namespace std;
string a,b;
int n;
typedef struct node
{
string data;
node*left,*right;
int num;
} tree;
tree*creat(tree*root,string x)
{
if(root==NULL)
{
root=new tree;
root->data=x;
root->left=root->right=NULL;
root->num=1;
}
else
{
if(root->data==x)
root->num++;
if(x<root->data)
root->left=creat(root->left,x);
if(x>root->data)
root->right=creat(root->right,x);
}
return root;
}
void mid(tree*root)
{
if(root)
{
mid(root->left);
cout<<root->data<<' ';printf("%.2f%%\n",root->num*1.0*100/n);
///cout<<root->data<<' '<<setiosflags(ios::fixed)<<setprecision(2)<<(1.0*100*root->num)/n<<'%'<<endl;
mid(root->right);
}
}
int main()
{
tree*root=NULL;
scanf("%d",&n);
getchar();
for(int i=0; i<n; i++)
{
getline(cin,a);
for(int i=0;a[i]!='\0';i++)
{
if(a[i]>='A'&&a[i]<='Z')
a[i]+=32;
}
root=creat(root,a);
}
mid(root);
return 0;
}