小白书79页对字符串和字符的排序

来源:互联网 发布:淘宝保证金诈骗 编辑:程序博客网 时间:2024/05/22 20:21
#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;

}

关于sort函数以及qsort函数的具体差别网上博客说的也不是很清楚,此题本想改成sort的用法,能力不够,改不对,因此还是用qsort

0 0
原创粉丝点击