UVA 10815

来源:互联网 发布:linux查看请求url 编辑:程序博客网 时间:2024/06/05 02:06

把小白书的5.3.字母重排搞懂了,这道题基本没问题。

主要考查字符比较函数和字符串比较函数是否熟练。


字符比较函数:

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);

}

附代码:


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define len 5001
#define le 201
char s[len*le][le];
int icmp(const void *a,const void *b)
{
    return strcmp((char *)a,(char *)b);
}
int main(void)
{
    int i=0,j=0;
    char ch;
    //freopen("D:\\in.txt","r",stdin);
    while((ch=getchar())!=EOF)
    {
        if(isalpha(ch))
            s[i][j++]=tolower(ch);
        if(!isalpha(ch)&&isalpha(s[i][0]))
        {
            s[i++][j]='\0';
            j=0;
        }
   }
   qsort(s,i,sizeof s[0],icmp);
   for(j=1,puts(s[0]);j<i;j++)
   if(strcmp(s[j-1],s[j]))
       puts(s[j]);
   return 0;
}

0 0
原创粉丝点击