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

紧急求助 这道题MLE

Posted by textdzy at 2009-04-08 13:00:06 on Problem 2418
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct tree
{
	char s[35];
	int count;
	struct tree*left;
	struct tree*right;
}tree;
typedef tree* tp;
int n=0;       
void ins(tp *a,char*s)
{
	tp tmp,tf;
	tmp=(tp)malloc(sizeof(tree));
	strcpy(tmp->s,s);
	tmp->count=1;
	tmp->left=NULL;
	tmp->right=NULL;
	int use;
	tp from=*a,rp;
	if (!from)
	{
		*a=tmp;
		return;
	}
    while (from)
    {
    	use=strcmp(from->s,s);
    	if (!use)
    	{
    		from->count++;
    		return;
    	}
    	rp=from;
    	from=(use<0)?from->right:from->left;
    }
    if (use<0)
        rp->right=tmp;
    else
        rp->left=tmp;
}	   
void inordertraverse(tp head)
{
	if (head)
	{
		inordertraverse(head->left);
		printf("%s %.4lf\n",head->s,head->count*100.0/n*1.0);
		inordertraverse(head->right);
	}
}
void del(tp head)
{
	if (head)
	{
		del(head->left);
		del(head->right);
		free(head);
	}
}
int main()
{
	int i;
	tp head=NULL;
	char str[35];
	while (gets(str))
	{
		n++;
		ins(&head,str);
	}
	inordertraverse(head);
	del(head);
}
	

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