题目描述:输入一个字典(用******结尾),然后再输入若干单词,每输入一个单词w,你都需要在字典中找出所有可以用w的字母重排后得到的单词,并按照字典序从小到大的顺序在一行中输出(如果不存在,输出:()。输入单词之间用空格或空行隔开,且所有输入单词都由不超过6个小写字母组成。注意,字典中的单词不一定按字典排序。
样例输入:tarp given score only trap work earn course pepper part
******
resco nfudre aptr sett oresuc
样例输出:
score
refund
part tarp trap
:(
course
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
char word[2000][10],sorted[2000][10];
//字符比较函数
int cmp_char(const void *a,const void *b)
{
char *x=(char *)a;
char *y=(char *)b;
return *x-*y;
}
//字符串比较函数
int cmp_string(const void *a,const void *b)
{
char *x=(char *)a;
char *y=(char *)b;
return strcmp(x,y);
}
int main()
{
int n=0;
for(;;){
scanf("%s",word[n]);
if(word[n][0]=='*') break;
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);//给所有单词排序
for(int i=0;i<n;i++){
strcpy(sorted[i],word[i]);
qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char);//给每个单词排序
}
char s[10];
while(scanf("%s",s)==1){
qsort(s,strlen(s),sizeof(char),cmp_char);
int found=0;
for(int i=0;i<n;i++){
if(strcmp(sorted[i],s)==0){
found=1;
printf("%s ",word[i]);
}
}
if(!found) printf(":(");
printf("\n");
}
return 0;
}
418

被折叠的 条评论
为什么被折叠?



