POJ 2418
1 #include <iostream> 2 #include <iomanip> 3 #include <string> 4 using namespace std; 5 6 struct node 7 { 8 string s; 9 node * l; 10 node * r; 11 double sum; 12 node() 13 { 14 l = NULL; 15 r = NULL; 16 sum = 0; 17 } 18 }; 19 20 double all; 21 22 node * insert(string s,node * root) 23 { 24 if(root == NULL) 25 { 26 root = new node(); 27 root->s = s; 28 root->sum ++; 29 return root; 30 } 31 if(s == root->s) 32 { 33 root->sum ++; 34 } 35 else if(s < root->s) 36 { 37 root->l = insert(s,root->l); 38 } 39 else if(s > root->s) 40 { 41 root->r = insert(s,root->r); 42 } 43 return root; 44 } 45 46 void tra_2(node * root) 47 { 48 if(root->l != NULL) 49 { 50 tra_2(root->l); 51 } 52 // if(root->s != "") 53 cout<<root->s<<" "<<setiosflags(ios::fixed)<<setprecision(4)<<(root->sum*100/all)<<endl; 54 // cout<<root->s<<root->sum<<endl; 55 if(root->r != NULL) 56 { 57 tra_2(root->r); 58 } 59 } 60 61 int main() 62 { 63 //freopen("acm.acm","r",stdin); 64 string s; 65 all = 0; 66 node * root = NULL; 67 while(getline(cin,s),s != "") 68 { 69 //getchar(); 70 ++ all; 71 root = insert(s,root); 72 } 73 // -- all; 74 // cout<<all<<endl; 75 tra_2(root); 76 }