# 5-24 树种统计 (25分)

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%

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
/* 评测结果

2016-08-30 09:22    答案正确    25  5-24    gcc     501     13  569985011

//C语言stricmp()函数:比较字符串(不区分大小写
#define lowcase(x) ((x)>='a'&&(x)<='z')
#define capitol(x) ((x)>='A'&&(x)<='Z')

typedef struct node *BinTree;
struct node {
char Data[33];
int cnt;
BinTree Left;
BinTree Right;
};

BinTree InsertT(BinTree BT,char*);
BinTree BuildT(char*);
void PrintT(BinTree,int);
char*scan(void){
char*a=(char*)malloc(sizeof(char)*33);
int i=0;
char c;
while((c=getchar())!='\n'){
a[i++]=c;
}
a[i]='\0';
return a;
}

int cmp(char*a,char*b){
for(;*a!='\0';a++,b++){
if(lowcase(*a)){
if(capitol(*b)){
if(*b+32==*a)continue;
else return *a-*b;
}
if(*b==*a)continue;
else return *a-*b;
}else{
if(lowcase(*b)){
if(*b-32==*a)continue;
else return *a-*b;
}
if(*b==*a)continue;
else return *a-*b;
}
}
return *a-*b;
}

int main() {

int n;
scanf("%d",&n);
getchar();
char *a;
BinTree BT=NULL;
for(int i=0; i<n; i++) {
a=scan();
BT=InsertT(BT,a);
//  printf("\n------%d----------\n",i+1);
}

PrintT(BT,n);

return 0;
}
void PrintT(BinTree BT,int n) {
if(BT) {
PrintT(BT->Left ,n);
double percent=(double)BT->cnt*100.0/n;
printf("%s %0.4lf%%\n",BT->Data ,percent);
//      printf("{%d}",BT->cnt );
PrintT(BT->Right,n );
}
}
BinTree InsertT(BinTree BT,char*name) {
while(BT) {
int flag=cmp(BT->Data ,name);
if(flag<0) {
if(BT->Right)BT=BT->Right ;
else {
BT->Right =BuildT(name);
}
}else
if(flag>0) {
if(BT->Left )BT=BT->Left ;
else {
BT->Left =BuildT(name);
}
}else{
BT->cnt+=1 ;
//          printf("[]");
}
}

return BuildT(name);
}

BinTree BuildT(char*Data) {
BinTree New=(BinTree)malloc(sizeof(struct node));
strcpy(New->Data ,Data);
New->cnt =1;
New->Left =NULL;
New->Right =NULL;
return New;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：5-24 树种统计 (25分) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)