博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2418 Hardwood Species
阅读量:6969 次
发布时间:2019-06-27

本文共 2183 字,大约阅读时间需要 7 分钟。

题意:给定一些树,按字典序输出数名和树出现的频率;

思路:这个题用二叉搜索树可以做,同时在网上看到了一个简单的方法,用map来做,这里map真是太好用了;

map做法:

View Code
#include 
#include
#include
#include
using namespace std; int main() {
string a; map
tree; double n = 0; char ss[35] = {NULL}; while(gets(ss)) {
a = ss; ++tree[a]; ++n; } map
::iterator temp; for(temp = tree.begin(); temp != tree.end(); ++temp) { cout<
first<<" "; printf("%.4f\n",(temp->second)*100.0/n); } return 0; }

二叉搜索树:

刚开始用stirng存名字,G++就是tle而c++就是CE ,无语了;

代码:

View Code
#include 
#include
#include
#include
using namespace std; struct node {
char s[40]; int n; node *left,*right; }; int n = 0; node *get() {
node *tt = NULL; tt = new node; tt->n = 1; tt->left = NULL; tt->right = NULL; return tt; } node *head = NULL; node *temp = NULL; char str[40] = {NULL}; void insert(char *str,node *t) {
if(strcmp(str,t->s) == 0) ++t->n; else if(strcmp(str,t->s) < 0) {
if(t->left == NULL) {
t->left = get(); t->left->n = 1; strcpy(t->left->s,str); return ; } else insert(str,t->left); } else {
if(t->right == NULL) {
t->right = get(); t->right->n = 1; strcpy(t->right->s,str); return ; } else insert(str,t->right); } } void output(node *tt) {
if(tt->left == NULL && tt->right == NULL) {
printf("%s %.4f\n",tt->s,tt->n*100.00/n); return ; } if(tt->left) output(tt->left); printf("%s %.4f\n",tt->s,tt->n*100.00/n); if(tt->right) output(tt->right); } int main() {
//freopen("input.txt","r",stdin); gets(str); head = get(); strcpy(head->s,str); head->n = 1; //puts(head->s); n = 1; while(gets(str) != NULL) {
++n; insert(str,head); } //printf("%d\n",n); output(head); return 0; }

转载于:https://www.cnblogs.com/LT-blogs/archive/2012/03/23/2414181.html

你可能感兴趣的文章
CSS样式中设置table的cellspacing属性
查看>>
The method getTextContent() is undefined for the type Node
查看>>
iPhone动画属性详解
查看>>
fatal error: 'openssl/err.h' file not found
查看>>
zabbix实现 SAS 6/iR 型号 Raid信息监控
查看>>
RHEL Centos7 Yum网络源与光盘源设置
查看>>
一条sql语句实现一维表生成二维表格
查看>>
我的友情链接
查看>>
从“赢”字诠释解读成功的必备要素(一)
查看>>
面试心得
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
2018-08-16期 HBase全分布模式集群及HMaster HA安装部署
查看>>
docker中的容器互联-linking系统
查看>>
Linux学习之CentOS(二十一)--Linux系统启动详解
查看>>
escape()、encodeURI()、encodeURIComponent()区别详解
查看>>
AgileEAS.NET5.0-界面设计器-使用说明书(上)
查看>>
g80 architecture overview
查看>>
gradle替代maven
查看>>
Linux命令之diff
查看>>