Data structure experiment search 3: tree species statistics
Time Limit: 400 ms Memory Limit: 65536 KiB
Problem Description
With the application of satellite imaging technology, natural resource research institutions can identify the type of each tree. Please write a program to help researchers count the number of each tree and calculate the percentage of each tree in the total.
Input
Enter a set of test data. The first line of the data gives a positive integer N (N < = 100000), N represents the number of trees; then N lines, each line gives the type name of a tree observed by the satellite. The name of the tree is a string of no more than 20 characters. The string consists of English letters and spaces, regardless of case.
Output
Output all kinds of tree names and their percentages in dictionary order, with spaces in the middle and two decimal places after the decimal point.
Sample Input
2 This is an Appletree this is an appletree
Sample Output
this is an appletree 100.00%
Hint
Source
xam
Binary sort tree
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char str[22]; int cnt; struct node *left; struct node *right; }; int n; struct node *creat(struct node *head, char *s) { if(!head) { head = (struct node *)malloc(sizeof(struct node)); head -> cnt = 1; strcpy(head -> str, s); head -> left = NULL; head -> right = NULL; } else { int k; k = strcmp(head -> str, s); if(k > 0) { head -> left = creat(head -> left, s); } else if(k < 0) head -> right = creat(head -> right, s); else head -> cnt++; } return head; }; void show(struct node *head) { if(head) { show(head -> left); printf("%s %.2lf%c\n", head -> str, head -> cnt * 100.0 / n, '%'); show(head -> right); } } int main() { struct node *head = NULL; char s[22]; scanf("%d", &n); getchar(); //Line feed int i, j; for(i = 0; i < n; i++) { gets(s); for(j = 0; s[j]; j++) { if(s[j] >= 'A' && s[j] <= 'Z') // All changed to lowercase s[j] += 32; } head = creat(head, s); } show(head); return 0; }