PTA-数据结构 5-24 树种统计 (25分)
2016-11-28 19:17
1396 查看
随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。
输入首先给出正整数N(\le 10^5≤105),随后N行,每行给出卫星观测到的一棵树的种类名称。种类名称由不超过30个英文字母和空格组成(大小写不区分)。
按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位。
思路分析:利用结构体存储树的种类,map记录映射关系。
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#define MAX 100000 + 10
using namespace std;
typedef struct {
string name;
int num;
} Tree;
Tree tree[MAX];
int member = 1;
map<string, int> mp;
void findTree( string name ) {
if( mp[name] == 0 ) {
mp[name] = member;
tree[member].name = name;
tree[member].num++;
member++;
}
else {
int id = mp[name];
tree[id].num++;
}
}
int cmp( Tree a, Tree b ) {
return a.name < b.name ? 1 : 0;
}
int main() {
//freopen( "123.txt", "r", stdin );
int n;
scanf( "%d", &n );
char ch = getchar();
string name;
for( int i = 0; i < n; i++ ) {
getline( cin, name );
findTree( name );
}
sort( tree + 1, tree + member, cmp );
for( int i = 1; i < member; i++ ) {
printf( "%s %.4lf%%\n", tree[i].name.c_str(), (double)tree[i].num * 100.0 / n );
}
return 0;
}
输入格式:
输入首先给出正整数N(\le 10^5≤105),随后N行,每行给出卫星观测到的一棵树的种类名称。种类名称由不超过30个英文字母和空格组成(大小写不区分)。
输出格式:
按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位。
输入样例:
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%
思路分析:利用结构体存储树的种类,map记录映射关系。
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#define MAX 100000 + 10
using namespace std;
typedef struct {
string name;
int num;
} Tree;
Tree tree[MAX];
int member = 1;
map<string, int> mp;
void findTree( string name ) {
if( mp[name] == 0 ) {
mp[name] = member;
tree[member].name = name;
tree[member].num++;
member++;
}
else {
int id = mp[name];
tree[id].num++;
}
}
int cmp( Tree a, Tree b ) {
return a.name < b.name ? 1 : 0;
}
int main() {
//freopen( "123.txt", "r", stdin );
int n;
scanf( "%d", &n );
char ch = getchar();
string name;
for( int i = 0; i < n; i++ ) {
getline( cin, name );
findTree( name );
}
sort( tree + 1, tree + member, cmp );
for( int i = 1; i < member; i++ ) {
printf( "%s %.4lf%%\n", tree[i].name.c_str(), (double)tree[i].num * 100.0 / n );
}
return 0;
}
相关文章推荐
- PTA-数据结构 5-34 任务调度的合理性 (25分)
- PTA-数据结构 5-42 整型关键字的散列映射 (25分)
- PTA-数据结构 5-26 Windows消息队列 (25分)
- PTA-数据结构 5-35 城市间紧急救援 (25分)
- PTA 7-24 树种统计(25 分)Map应用拓展
- PTA 数据结构与算法题目集(中文)5-4 是否同一棵二叉搜索树 (25分)
- PTA-数据结构 5-18 银行业务队列简单模拟 (25分)
- PTA-数据结构 5-29 修理牧场 (25分)
- PTA 数据结构与算法题目集(中文)5-8 哈利·波特的考试 (25分)
- PTA 数据结构与算法题目集(中文)5-9 旅游规划 (25分)
- 5-24 树种统计 (25分)
- PTA 数据结构与算法题目集(中文)5-5 堆中的路径 (25分)
- PTA-数据结构 5-45 航空公司VIP客户查询 (25分)
- PTA-数据结构 5-38 寻找大富翁 (25分)
- PTA-数据结构 5-31 笛卡尔树 (25分)
- 5-24 树种统计 (25分)
- 数据结构实验之查找三:树的种类统计
- 数据结构实验之二叉树三:统计叶子数
- pta-数据结构与算法题目集(中文)-4-2 顺序表操作集
- 数据结构实验之查找三:树的种类统计