题目:查找单词是否在字典中出现。
分析:简单题、排序。全转化成小写排序,去重后在排序输出。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char word[ 21 ];
char sort[ 21 ];
bool same;
}node;
node data[ 1001 ];
int cmp0( const void* a, const void* b )
{
return *((char *)a) - *((char *)b);
}
int cmp1( const void* a, const void* b )
{
node *p = (node *)a;
node *q = (node *)b;
return strcmp( p->sort, q->sort );
}
int cmp2( const void* a, const void* b )
{
node *p = (node *)a;
node *q = (node *)b;
if ( p->same == q->same )
return strcmp( p->word, q->word );
return p->same - q->same;
}
int main()
{
memset( data, 0, sizeof( data ) );
int count = 0;
while ( scanf("%s",data[count].word) && strcmp( data[count].word, "#" ) ) {
strcpy( data[count].sort, data[count].word );
for ( int i = strlen(data[count].sort)-1 ; i >= 0 ; -- i )
if ( data[count].sort[i] >= 'A' && data[count].sort[i] <= 'Z' )
data[count].sort[i] += 'a'-'A';
qsort( data[count].sort, strlen(data[count].sort), sizeof(char), cmp0 );
++ count;
}
qsort( data, count, sizeof(node), cmp1 );
for ( int i = 1 ; i < count ; ++ i )
if ( !strcmp( data[i].sort, data[i-1].sort ) )
data[i].same = data[i-1].same = 1;
qsort( data, count, sizeof(node), cmp2 );
for ( int i = 0 ; i < count ; ++ i ) {
if ( data[i].same ) break;
printf("%s\n",data[i].word);
}
return 0;
}