字母重排--

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdlib>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
//字符比较函数
//void *则为"无类型指针"
//void *可以指向任何类型的数据。---注意注意注意,重要的事情说三遍
int cmp_char(const void* _a,const void* _b)
{
    char* a=(char*)_a;
    char* b=(char*)_b;
    return *a-*b;
}
//字符串比较函数
//void *可以指向任何类型的数据。---注意注意注意,重要的事情说三遍
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)==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;
}

来源:《算发竞赛入门经典》

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/81781892