UVa:10815 - Andy's First Dictionary

来源:互联网 发布:鼠标自动点击软件 编辑:程序博客网 时间:2024/06/16 15:36

用gets输入:

#include<stdio.h>#include<string.h>#include<cctype>#include<algorithm>using namespace std;char text[210],comp[210];struct Word{    char word[210];}w[5010];bool cmp(struct Word a,struct Word b){    return strcmp(a.word,b.word)<0;}int main(){    int t=0;    while(gets(text)!=NULL){        int len=strlen(text);        int j=0;        for(int i=0;i<=len;i++){            if(isalpha(text[i]))                comp[j++]=tolower(text[i]);            else if(isalpha(comp[0])){                comp[j]='\0';                int p=1;                for(int k=0;k<t;++k)                    if(strcmp(comp,w[k].word)==0){                        p=0; break;                    }                if(p){                    for(int k=0;k<=j;++k)                        w[t].word[k]=comp[k];                    ++t;                }                memset(comp,0,sizeof(comp));//记得将数组comp[]清空,否则虽然在编译器上运行正确,但提交后就是WA,这是隐含性的错误                j=0;            }        }    }    sort(w,w+t,cmp);    for(int i=0;i<t;++i)        printf("%s\n",w[i].word);    return 0;}

用scanf输入:

#include<stdio.h>#include<string.h>#include<cctype>#include<algorithm>using namespace std;char text,comp[210];struct Word{    char word[210];}w[5010];bool cmp(struct Word a,struct Word b){    return strcmp(a.word,b.word)<0;}int main(){    int t=0,j=0;;    while(scanf("%c",&text)!=EOF){        if(isalpha(text))            comp[j++]=tolower(text);        else if(isalpha(comp[0])){            comp[j]='\0';            int p=1;            for(int k=0;k<t;++k)                if(strcmp(comp,w[k].word)==0){                    p=0; break;                }            if(p){                for(int k=0;k<=j;++k)                    w[t].word[k]=comp[k];                ++t;            }            memset(comp,0,sizeof(comp));//记得将数组comp[]清空,否则虽然在编译器上运行正确,但提交后就是WA,这是隐含性的错误            j=0;        }    }    sort(w,w+t,cmp);    for(int i=0;i<t;++i)        printf("%s\n",w[i].word);    return 0;}

--------------------------------------------------------------------------------------------

          Keep It Simple,Stupid!

--------------------------------------------------------------------------------------------

0 0
原创粉丝点击