Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用二叉排序树,为啥G++WA,C++AC啊

Posted by NKU_Happy at 2010-09-07 17:06:46 on Problem 2418
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator