寒假刷题之3——小朋友来数数
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.
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 ...
Output
2
7
10
9
复制这些内容就够蛋疼啊。。。
就是普通的计算单词数目,以前好像写过,很快就写了一段:
#include<stdio.h> int main() { char letter; int i = 0; for (; (scanf("%c", &letter)) != EOF; letter == ' ' ? i ++ : NULL) letter == '\n'? (printf("%d\n", i + 1), i = 0) : NULL; printf("%d\n", i + 1); return 0; }
传上去竟然Wrong answer了。。。
好吧我认真地看了原题目,连续的字母算一个单词,我是按空格数+1来算的。
改代码吧。。。
完全改掉代码,换思路来写,用isalpha判断第一个字符是不是字母,然后读取后面的字母直到其他字符,继续累积。
没想到还给wa了两次,都是我不小心,忘记把i初始化。
现在终于ac了,虽然不是很好的代码。。。贴出代码:
#include<stdio.h> #include<ctype.h> int main() { char letter; int i = 0; for (; (scanf("%c", &letter)) != EOF;){ if (isalpha(letter)){ i ++; while (isalpha(letter)) scanf("%c", &letter); } if (letter == '\n'){ printf("%d\n", i); i = 0; } } return 0; }嗯,这次就这样吧。要休息一会了,不然就太累了。。。