Poj 1002 487 - 3279 (이 진 트 리 검색)
9716 단어 poj
사고방식 분석: 먼저 입력 문 자 를 처리 하고 표준 형식 으로 전환한다.표준 형식의 전화 번 호 를 검색 트 리 에 삽입 하고 같은 번호 카운터 가 1 을 추가 하면 중간 순서 로 검색 트 리 를 옮 겨 다 닙 니 다.
코드 는 다음 과 같 습 니 다:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TreeNode;
typedef char ElementType[30];
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 = 0;
int main()
{
int n;
char Str[100], Tel[100];
SearchTree T = NULL;
menset( Tel, 0, sizeof(Tel) );
scanf( "%d", &n );
for ( int i = 0; i < n; ++i )
{
scanf( "%s", Str );
StrToNum( Str, Tel );
T = Insert( Tel, T );
}
PrintTel( T );
if ( flag == 0 )
printf( "No duplicates.
" );
return 0;
}
void PrintTel( SearchTree T )
{
if ( T != NULL )
{
PrintTel( T->Left );
if ( T->Count > 1 )
{
printf( "%s %d
", T->Element, T->Count );
flag = 1;
}
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) < 0 )
T->Left = Insert( X, T->Left );
else
if ( strcmp(X, T->Element) > 0 )
T->Right = Insert( X, T->Right);
return T;
}
void StrToNum( char *str , char * Tel )
{
int i, j;
for ( i = j = 0; str[i] != '\0'; i++ )
{
if ( str[i] == '-' );
else
if ( '0' <= str[i] && str[i] <= '9' )
Tel[j++] = str[i];
else
if ( str[i] < 'Q' )
Tel[j++] = ( str[i] - 'A' ) / 3 + 2 + '0';
else
Tel[j++] = ( str[i] - 'A' - 1 ) / 3 + 2 + '0';
if ( j == 3 )
Tel[j++] = '-';
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ3071: Football(확률 DP)Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. After n rounds, only one team...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.