| ||||||||||
| 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 | |||||||||
请高手指点一下,我采用树结构,给我提供一个我这个程序过不了的测试用利#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
struct tnode *addtreex(struct tnode*,char *);
void treexprint(struct tnode*);
struct tnode{
char s[10];
int times;
struct tnode *left;
struct tnode *right;
};
int total=0;
char t[8];//save the converted string
struct tnode *talloc(void)
{
return (struct tnode *)malloc(sizeof(struct tnode));
}
struct tnode *addtreex(struct tnode *p,char *w)
{
int cond;
if(p==NULL){
p=talloc();
strcpy(p->s,w);
p->times=1;
p->left=p->right=NULL;
}
else if((cond=strcmp(w,p->s))==0)
(p->times)++;
else if(cond<0)
p->left=addtreex(p->left,w);
else
p->right=addtreex(p->right,w);
return p;
}
void treexprint(struct tnode *p)
{
int i;
if(p!=NULL){
treexprint(p->left);
if(p->times > 1)
{
total++;
for(i=0;i < 3;i++)
printf("%c",(p->s)[i]);
putchar('-');
for(;i < 7;i++)
printf("%c",(p->s)[i]);
putchar(' ');
printf("%d\n",p->times);
}
treexprint(p->right);
}
}
chuli(char *s)
{
int i;
int point_t=0;
for(i=0;s[i];i++)
{
if(isdigit(s[i]))
t[point_t++]=s[i];
else if(s[i]>=65 && s[i]<=67)
t[point_t++]='2';
else if(s[i]>=68 && s[i]<=70)
t[point_t++]='3';
else if(s[i]>=71 && s[i]<=73)
t[point_t++]='4';
else if(s[i]>=74 && s[i]<=76)
t[point_t++]='5';
else if(s[i]>=77 && s[i]<=79)
t[point_t++]='6';
else if(s[i]==80 || s[i]>=82 && s[i]<=83)
t[point_t++]='7';
else if(s[i]>=84 && s[i]<=86)
t[point_t++]='8';
else if(s[i]>=87 && s[i]<=89)
t[point_t++]='9';
else
;
}
t[point_t]='\0';
}
int main()
{
int n;
char i;
char s[20];
struct tnode *root;
root=NULL;
scanf("%d",&n);
gets(s);//read the rest of the first line
for(i=0;i<n;i++)
{
gets(s);
chuli(s);
root=addtreex(root,t);
}
if(n!=0)
treexprint(root);
if(total==0)
printf("No duplicates. \n");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator