UVa494 - Kindergarten Counting Game-难度1

来源:互联网 发布:淘宝怎样重新申请退款 编辑:程序博客网 时间:2024/06/05 16:26

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=435

                                
                                 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 on a 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

代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cctype>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8. #ifdef LOCAL  
  9.     freopen("f:\\input.txt""r", stdin);  
  10. #endif LOCAL  
  11.     char buf[10000];  
  12.     while(fgets(buf, sizeof(buf), stdin))  
  13.     {  
  14.         int count = 0, flag = 1;  
  15.         for(int i = 0; buf[i] != 0 ; ++i)  
  16.         {  
  17.             if(flag && isalpha(buf[i]))  
  18.             {//当flag为1并且buf[i]是字母时,count+1  
  19.                 count ++;  
  20.                 flag = 0;//当碰到一次字母后把flag置为0  
  21.             }  
  22.             if(!isalpha(buf[i]))  
  23.             {  
  24.                 flag = 1;//当碰到一次非字母后把falg置为1  
  25.             }  
  26.         }  
  27.         cout << count << endl;  
  28.     }  
  29.     return 0;  
  30. }  
0 0
原创粉丝点击