2013 gzhu acm

題目描述:ios

                   Word Counting

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others)

Problem Description

Each year on May the graduate students are busy writing thesis, and the graduation thesis needs thousands of words. It's not a simple matter. Counting the words correctly become an interesting job.ide

We all know that the thesis may include English words, numbers, punctuation marks, images, formulas, and so on.ui

During the word count, we assume that English words, a string of meaningful numbers and punctuation mark is valid statistics word.spa

The single character in English words or numbers can not be counted as a word.rest

For example, the word "acm" count as one word instead of three words, number "2011" counted as a word. Of course, such space and carriage returns(Enter) can't be considered a word.code

Write a program to help graduates to test whether the number of words under the thesis requirement.orm

Input

There are multiple test case in input, each test case end with a single line "###", the input may contain english character, numbers, punctuation e.g. ':' , ',' , '+' , '-', and space, Enter.blog

Output

Output the number of word for each test case, and a separate line for each case.three

Sample Input

A simple test
###
Hunan University 2011 the 7th Programming Contest.
###
The 5th Central South China Programming Contest.
###

Sample Output

3
8
8

Source

HNU Contest 

    這個題目要注意的是,單個的標點符號算做一個單詞。

     A simple+-.ui test.    答案是:8   (simple+-.ui:simple  +  -  .  ui 共5個)ip

     

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <ctype.h>
 6 using namespace std;
 7 
 8 const int maxn = 1000 + 10;
 9 char s[maxn];
10 
11 int main()
12 {
13     int i, len, cnt = 0;
14     while (scanf("%s", s) != EOF)
15     {
16           if (strcmp(s, "###") == 0)
17           {
18                printf("%d\n", cnt);
19                cnt = 0;
20           }
21           else{
22                 len = strlen(s);
23                 bool mark = false;
24                 for (i = 0; i < len; i++)
25                 {
26                     if (ispunct(s[i]))
27                     {
28                         cnt++;
29                         mark = false;
30                     }
31                     else if (!mark)
32                     {
33                         cnt++;
34                         mark = true;
35                     }
36                 }
37           }
38     }
39     return 0;
40 }
相關文章
相關標籤/搜索