日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C - 数据结构实验之查找三:树的种类统计(哈希树)

發布時間:2025/3/21 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C - 数据结构实验之查找三:树的种类统计(哈希树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description

隨著衛星成像技術的應用,自然資源研究機構可以識別每一個棵樹的種類。請編寫程序幫助研究人員統計每種樹的數量,計算每種樹占總數的百分比。
Input

輸入一組測試數據。數據的第1行給出一個正整數N (n <= 100000),N表示樹的數量;隨后N行,每行給出衛星觀測到的一棵樹的種類名稱,樹的名稱是一個不超過20個字符的字符串,字符串由英文字母和空格組成,不區分大小寫。
Output

按字典序輸出各種樹的種類名稱和它占的百分比,中間以空格間隔,小數點后保留兩位小數。
Sample
Input

2 This is an Appletree this is an appletree

Output

this is an appletree 100.00%

Hint

哈希樹
字典樹

#include<bits/stdc++.h>using namespace std;typedef struct node {char name[30];int cnt;struct node *l, *r; } tree;int n; tree *create(tree *&root, char str[]) {if(root == NULL){root = new tree;strcpy(root->name, str);root->cnt = 1;root->l = NULL;root->r = NULL;}else{int temp = strcmp(root->name, str);if(temp > 0)create(root->l, str);else if(temp < 0)create(root->r, str);else root->cnt++;}return root; }void midorder(tree *root) {if(root){midorder(root->l);printf("%s %.2lf%%\n", root->name, 100.0 * root->cnt / n);midorder(root->r);} } int main() {//ios::sync_with_stdio(0);不能加這條語句,加了不給過int t;char s[30];tree *root;root = NULL;cin >> t;n = t;getchar();while(t--){gets(s);int len = strlen(s);for(int i = 0; i < len; i++){if(s[i] >= 'A' && s[i] <= 'Z')s[i] += 'a' - 'A';}root = create(root, s);}midorder(root);return 0; }

總結

以上是生活随笔為你收集整理的C - 数据结构实验之查找三:树的种类统计(哈希树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。