首页 技术 正文
技术 2022年11月14日
0 收藏 997 点赞 4,321 浏览 1474 个字

题目链接:http://poj.org/problem?id=1002

思路分析:先对输入字符进行处理,转换为标准形式;插入标准形式的电话号码到查找树中,若有相同号码计数器增加1,再中序遍历查找树。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct TreeNode;
typedef char ElementType[];
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
struct TreeNode
{
int Count;
ElementType Element;
SearchTree Left;
SearchTree Right;
};void StrToNum( char *str , char * Tel );
SearchTree MakeEmpty( SearchTree T );
SearchTree Insert( ElementType X, SearchTree T );
void PrintTel( SearchTree T );
int flag = ;int main()
{
int n;
char Str[], Tel[];
SearchTree T = NULL; menset( Tel, , sizeof(Tel) ); scanf( "%d", &n );
for ( int i = ; i < n; ++i )
{
scanf( "%s", Str );
StrToNum( Str, Tel );
T = Insert( Tel, T );
} PrintTel( T );
if ( flag == )
printf( "No duplicates.\n" ); return ;
}void PrintTel( SearchTree T )
{
if ( T != NULL )
{
PrintTel( T->Left );
if ( T->Count > )
{
printf( "%s %d\n", T->Element, T->Count );
flag = ;
}
PrintTel( T->Right );
}
}SearchTree MakeEmpty( SearchTree T )
{
if ( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}SearchTree Insert( ElementType X, SearchTree T )
{
if ( T == NULL )
{
T = ( Position )malloc( sizeof( struct TreeNode ) );
if ( T == NULL )
{
printf( "Out of space" );
return NULL;
}
else
{
strcpy( T->Element, X );
T->Left = T->Right = NULL;
}
}
else
if ( strcmp(X, T->Element) < )
T->Left = Insert( X, T->Left );
else
if ( strcmp(X, T->Element) > )
T->Right = Insert( X, T->Right); return T;
}void StrToNum( char *str , char * Tel )
{
int i, j; for ( i = j = ; str[i] != '\0'; i++ )
{
if ( str[i] == '-' );
else
if ( '' <= str[i] && str[i] <= '' )
Tel[j++] = str[i];
else
if ( str[i] < 'Q' )
Tel[j++] = ( str[i] - 'A' ) / + + '';
else
Tel[j++] = ( str[i] - 'A' - ) / + + ''; if ( j == )
Tel[j++] = '-';
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848