#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
int cmp_char(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return *a-*b;
}
int cmp_string(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int main()
{
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))
{
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;
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
int cmp_char(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return *a-*b;
}
int cmp_string(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int main()
{
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))
{
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;
}
关于sort函数以及qsort函数的具体差别网上博客说的也不是很清楚,此题本想改成sort的用法,能力不够,改不对,因此还是用qsort
本文通过一个具体的程序实例对比了C++中qsort与sort函数的应用。该程序实现了对字符串数组进行排序,并对每个字符串内的字符进行单独排序。尽管作者尝试将qsort替换为sort但未成功,该示例仍展示了qsort的强大功能。

7832

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



