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%
方法一:
#include "iostream" #include "iomanip" #include "string" #include "string.h" using namespace std; struct Node { string str; int count; Node *left,*right; Node() { str=' '; count=0; left=NULL; right=NULL; } }; void Insert(Node *&p,string A) { if(p==NULL) { p=new Node; p->str=A; p->count=1; } else if(p->str<A) { Insert(p->right,A); } else if(p->str>A) { Insert(p->left,A); } else { p->count++; } } void Cal(Node *p,int n) { if(p==NULL) return ; else { Cal(p->left,n); cout<<fixed<<setprecision(4)<<p->str<<" "<<(p->count*100.0)/n<<endl; Cal(p->right,n); } } int main() { //freopen("1.txt","r",stdin); string A; Node *root; root=NULL; int num=0; char str[100]; while(gets(str)) { num++; Insert(root,str ); } Cal(root,num); return 0; }
方法二:
#include "iostream" #include "iomanip" #include "string" #include "map" #include "string.h" using namespace std; map<string,int>tree; int main() { //freopen("1.txt","r",stdin); tree.clear(); char str[50]; int count=0; while(gets(str)) { count++; if(tree.count(str)==0) { tree[str]=1; } else { tree[str]++; } } map<string,int>::iterator it; for(it=tree.begin();it!=tree.end();it++) { cout<<fixed<<setprecision(4)<<it->first<<" "<<(it->second*100.0)/count<<"%"<<endl; } return 0; }