uvaRoot :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 0. Getting StartedSubmit Do

来源:互联网 发布:编程公司 编辑:程序博客网 时间:2024/06/10 08:34

 Kindergarten Counting Game 

Everybody sit down in a circle. Ok. Listen to me carefully.

``Woooooo, you scwewy wabbit!''

Now, could someone tell me how many words I just said?

Input and Output

Input to your program will consist of a series of lines, each line containing multiple words (at least one).A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).

Your program should output a word count for each line of input. Each word count should be printed ona separate line.

Sample Input

Meep Meep!I tot I taw a putty tat.I did! I did! I did taw a putty tat.Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...

Sample Output

27109


wa:#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;const int MAXN = 205;string s;char ch, a;bool is(char ch){    return ('a'<=ch&&ch<='z' || 'A'<=ch&&ch<='Z');}int main(){    //freopen("input.txt", "r", stdin);    while(getline(cin , s))    {        int cnt = 0;        int len = s.length();        for(int i = 0; i<len-1; i++)        {            if(s[i] == ' ')            {                while(s[i+1] == ' ') i++;                if(is(s[i+1]))                    cnt++;            }        }        printf("%d\n", cnt+1);    }    return 0;}#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;const int MAXN = 205;string s;char ch, a;bool is_zimu(char ch){    return ('a'<=ch&&ch<='z' || 'A'<=ch&&ch<='Z');}bool is(string s){    int len = s.length();    for(int i = 0; i< len; i++)    {        if(is_zimu(s[i])) return true;    }    return false;}int main(){    //freopen("input.txt", "r", stdin);    int cnt = 0;    while((ch = getchar()) != EOF)    {        if(ch == '\n')        {            if(is(s))            {                cnt++;            }            printf("%d\n", cnt);            s.clear();            cnt = 0;continue;        }        if(ch == ' ')        {            if(is(s))            {                cnt++;            }            s.clear();continue;        }        s += ch;    }    return 0;}终于ac,其实想到了好的方法几行代码就ok,否则真是没办法啊#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;const int MAXN = 205;string s;char ch, a;int cnt = 0;bool is_zimu(char ch){    return ('a'<=ch&&ch<='z' || 'A'<=ch&&ch<='Z');}int main(){    //freopen("input.txt", "r", stdin);    while(getline(cin, s))    {        int len = s.length();         for(int i = 0; i < len-1; i++)        {            if(is_zimu(s[i]))            {                while(is_zimu(s[i]))                {                    i++;                }                cnt++;            }        }        printf("%d\n", cnt);        cnt = 0;    }    return 0;}#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cctype>//发现了一个好头文件,可以将用来判断字母using namespace std;const int MAXN = 205;string s;char ch, a;int cnt = 0;/*bool is_zimu(char ch){    return ('a'<=ch&&ch<='z' || 'A'<=ch&&ch<='Z');}*/int main(){    freopen("input.txt", "r", stdin);    while(getline(cin, s))    {        int len = s.length();         for(int i = 0; i < len-1; i++)        {            if(isalpha(s[i]))//判断是否是字母,不用自己写判断函数了            {                while(isalpha(s[i]))                {                    i++;                }                cnt++;            }        }        printf("%d\n", cnt);        cnt = 0;    }    return 0;}



0 0
原创粉丝点击