Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用二叉排序树,为啥G++WA,C++AC啊#include <iostream> #include <cstring> #include <cstdio> using namespace std; //poj 2418 查找树 struct NODE { char name[40]; NODE* lchild,*rchild; int count; }; int sum; bool SearchBST(NODE *T,char *key,NODE*f,NODE* &p) { if(!T) {p=f;return false;} int num = strcmp(key,T->name); if(num==0){p = T;return true;} else if(num<0) return SearchBST(T->lchild,key,T,p); else return SearchBST(T->rchild,key,T,p); } void InsertBST(NODE *&T,char *e) { NODE *p=NULL; if(SearchBST(T,e,NULL,p)) p->count++; else { NODE *s = new NODE; strcpy(s->name,e);s->lchild=s->rchild=NULL; s->count = 1; if(!p) T=s; else if(strcmp(e,p->name)<0) p->lchild = s; else p->rchild = s; } } void output(NODE *T) { if(!T) return; output(T->lchild); double ans = (double)((T->count)*100); ans = ans/((double)sum); printf("%s %.4lf\n",T->name,ans); output(T->rchild); } int main(void) { char temp[40]; NODE *T = NULL; sum = 0; while (gets(temp)) { sum++; InsertBST(T,temp); memset(temp,'\0',sizeof(temp)); } output(T); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator