poj 2418 Hardwood Species
生活随笔
收集整理的這篇文章主要介紹了
poj 2418 Hardwood Species
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
map // 題意:給出不同的樹,求每種樹所占的比例,并要求樹名按字典序由小到大輸出
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string,int> col;
char str[100];
double tot=0;
while(gets(str))
{
col[str]++;
tot+=1;
}
for(map<string,int>::iterator ite=col.begin();ite!=col.end();++ite)
{
printf("%s %.4lf\n",(ite->first).c_str(),ite->second*100/tot);
}
return 0;
}
#include <string>
using namespace std;
struct Node
{
char ch[35];
int num; //該結點出現次數
Node *left,*right;
}node[10010];
Node* root;
int cnt;
Node* newNode(char s[])
{
Node* u=&node[++cnt];
strcpy(u->ch,s);
u->num=1;
u->left=u->right=NULL;
return u;
}
void addNode(char s[]) //插入新結點
{
Node *u=root,*fa=root;
int tag;
while(u!=NULL)
{
if(strcmp(s,u->ch)==0) //檢測到相同串
{
u->num++;
break;
}
else if(strcmp(s,u->ch)<0) //插入到左子樹
{
fa=u; //記錄當前結點的父節點
u=u->left;
tag=1;
}
else //插入到右子樹
{
fa=u;
u=u->right;
tag=2;
}
}
if(u==NULL) //該串第一次出現
{
if(tag==1)
fa->left=newNode(s);
else
fa->right=newNode(s);
}
}
int tot; //結點總數
void m_order(Node* u) //中序遍歷
{
if(u->left)
m_order(u->left);
printf("%s %.4lf\n",u->ch,u->num*100.0/tot);
if(u->right)
m_order(u->right);
}
int main()
{
char s[35];
gets(s);
root=newNode(s);
tot=1;
while(gets(s))
{
addNode(s);
tot++;
}
m_order(root);
return 0;
}
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string,int> col;
char str[100];
double tot=0;
while(gets(str))
{
col[str]++;
tot+=1;
}
for(map<string,int>::iterator ite=col.begin();ite!=col.end();++ite)
{
printf("%s %.4lf\n",(ite->first).c_str(),ite->second*100/tot);
}
return 0;
}
?
?
?
#include <iostream> //二叉查找樹(二叉排序樹)#include <string>
using namespace std;
struct Node
{
char ch[35];
int num; //該結點出現次數
Node *left,*right;
}node[10010];
Node* root;
int cnt;
Node* newNode(char s[])
{
Node* u=&node[++cnt];
strcpy(u->ch,s);
u->num=1;
u->left=u->right=NULL;
return u;
}
void addNode(char s[]) //插入新結點
{
Node *u=root,*fa=root;
int tag;
while(u!=NULL)
{
if(strcmp(s,u->ch)==0) //檢測到相同串
{
u->num++;
break;
}
else if(strcmp(s,u->ch)<0) //插入到左子樹
{
fa=u; //記錄當前結點的父節點
u=u->left;
tag=1;
}
else //插入到右子樹
{
fa=u;
u=u->right;
tag=2;
}
}
if(u==NULL) //該串第一次出現
{
if(tag==1)
fa->left=newNode(s);
else
fa->right=newNode(s);
}
}
int tot; //結點總數
void m_order(Node* u) //中序遍歷
{
if(u->left)
m_order(u->left);
printf("%s %.4lf\n",u->ch,u->num*100.0/tot);
if(u->right)
m_order(u->right);
}
int main()
{
char s[35];
gets(s);
root=newNode(s);
tot=1;
while(gets(s))
{
addNode(s);
tot++;
}
m_order(root);
return 0;
}
轉載于:https://www.cnblogs.com/mjc467621163/archive/2012/03/14/2396733.html
總結
以上是生活随笔為你收集整理的poj 2418 Hardwood Species的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何检查对象的类型[iOS/Androi
- 下一篇: 2.12.ECMAScript--运算符