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

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).

Output

Your program should output a word count for each line of input. Each word count should be printedon 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

2

7

10

9

问题链接:UVA494 Kindergarten Counting Game。

题意简述

  幼儿园数单词游戏。输入若干句话,数一下每句有几个单词输出。

问题分析

  程序代码不够简洁又写了2个代码。

  本题解决方法有多种,一是用C语言的字符串函数strtok()来实现,二是用有限状态自动机的原理来实现,也可以用简单的字符流方式来实现。

  开始的时候,把焦点放在输入上,主要是考虑字母和非字母的处理(过滤),写了自己的字符输入处理程序,代码显得略微长,而且不够简洁。

  用C语言的字符串函数strtok()来实现时,需要过滤掉非字母的字符,剩下的逻辑就比较简单了。

  按照有限状态自动机的原理来实现是正解,代码见第1个程序。状态只需要2个,状态0表示尚未有字母字符或初始状态,状态1表示前一个读入的字符是字母。状态设计好之后,处理就简单了,根据输入的字符相应处理就可以了。

程序说明

  用字符流实现时,封装了函数mygetchar()和mygetwords(),使得程序逻辑更加简洁,可阅读行强,通用行好。

题记

  C语言程序员的一项重要工作就是封装功能函数。

AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include <bits/stdc++.h>using namespace std;int main()
{string s;while(getline(cin, s)) {int state = 0, cnt = 0;for(int i = 0; s[i]; i++) {if(state == 0) {if(isalpha(s[i])) {state = 1;cnt++;}} else {        // stete=1if(!isalpha(s[i]))state = 0;}}printf("%d\n", cnt);}return 0;
}

AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include <bits/stdc++.h>using namespace std;int main()
{char buf[1024];char delim[] = " ";int cnt;while(gets(buf) != NULL) {for(int i = 0; buf[i]; i++)buf[i] = isalpha(buf[i]) ? buf[i] : ' ';cnt = 0;char *p = strtok(buf, delim);while(p) {cnt++;p = strtok(NULL, delim);}printf("%d\n", cnt);}return 0;
}

AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include <stdio.h>
#include <ctype.h>
#include <string.h>char mygetchar()
{char c;c = getchar();if(c != '\n' && c != EOF)c = isalpha(c) ? c : ' ';return c;
}int mygetwords()
{char c;int count = 0;while((c = mygetchar()) && c != '\n' && c != EOF) {if(isalpha(c))count++;while(isalpha(c))c = mygetchar();}return (count == 0 && c == EOF) ? -1 : count;
}int main(void)
{int count;for(;;) {count = mygetwords();if(count < 0)break;printf("%d\n", count);}return 0;
}

另外一个AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include <stdio.h>
#include <ctype.h>
#include <string.h>int mygets(char s[])
{int i = 0;char c;while((c = getchar()) && c != '\n' && c != EOF)s[i++] = isalpha(c) ? c : ' ';s[i] = '\0';return i;
}int main(void)
{char buf[1024];char delim[] = " ";char *p;int count;while(mygets(buf)) {count = 0;p = strtok(buf, delim);while(p) {count++;p = strtok(NULL, delim);}printf("%d\n", count);}return 0;
}

UVA494 Kindergarten Counting Game【输入输出+水题】相关推荐

  1. UVA494 Kindergarten Counting Game

    C语言程序员的一项重要工作就是封装功能函数. 问题链接:UVA494 Kindergarten Counting Game. 题意简述:幼儿园数单词游戏.输入若干句话,数一下每句有几个单词输出. 问题 ...

  2. NUC1429 WERTYU【输入输出+水题】

    WERTYU 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 A common typing error is to place the hands o ...

  3. HDU1870 愚人节的礼物【堆栈+输入输出+水题】

    愚人节的礼物 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. UVA11988 Broken Keyboard (a.k.a. Beiju Text)【输入输出+水题】

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problemwit ...

  5. 洛谷P1352 没有上司的舞会(树形DP水题)

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  6. LQ0270 手机尾数【水题】

    题目来源:蓝桥杯2012初赛 C++ C组H题 题目描述 30 年的改革开放,给中国带来了翻天覆地的变化.2011 全年中国手机产量约为 11.72 亿部.手机已经成为百姓的基本日用品! 给手机选个好 ...

  7. P1339 [USACO09OCT]热浪Heat Wave(最短路水题)

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...

  8. LOJ #6303. 水题 (约数 质因数)

    #6303. 水题 内存限制 10 MiB 时间限制:1000 ms 标准输入输出 题目描述 给定正整数 n,kn, kn,k,已知非负整数 xxx 满足 n!modkx=0,求 xmaxx_{max ...

  9. 水题/poj 1852 Ants

    1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...

最新文章

  1. Nvidia TX2 安装中文输入法
  2. 业界资讯: Air 2.0 beta 版本 发布
  3. Xilinx的ISE14.7和PlanAhead与win10系统的兼容性问题解决方案
  4. SVM -支持向量机原理详解与实践之五
  5. 判断文件是否为可执行程序
  6. [云炬创业学笔记]第一章创业是什么测试14
  7. 2018年第九届蓝桥杯C/C++ A组国赛 —— 第二题:阅兵方阵
  8. mysql删除bin-log_删除MYSQl BIN-LOG 日志
  9. 1090. Highest Price in Supply Chain (25)
  10. 工作118:封装一个带有对话框的button组件
  11. VC++ 6.0 与VS2008 C++ DEBUG工具(Windows)介绍
  12. bundle 安装_超级小白使用pip安装第三方库的正确姿势
  13. 控制台下星号密码输入的实现
  14. sql server 架构_在SQL Server中引入架构文档
  15. 5G加速向纵深发展 中国电信联合产业链开展“5G创新终端商用合作行动”
  16. 标识符——Python
  17. mac VMware fusion配置nat网络
  18. 软件测试的维护,浅谈如何维护软件测试用例
  19. Octree 了然于胸
  20. 多元逻辑回归公式推导

热门文章

  1. MUI+Htmlplus开发APP实现页面之间传值
  2. xlwings,让excel飞起来!
  3. [Flex]Flex编程注意之自动获取焦点、监听全局键盘事件
  4. 【java学习之路】(mysql篇)003.mysql中limit、表的创建、删除、约束
  5. Linux 2.6 和安卓一样吗,Linux2.4和Linux2.6设备驱动的一些区别
  6. a标签js阻止跳转_前端笔试知识点:阻止冒泡
  7. jmeter校验结果_Springboot + redis + 注解 + 拦截器来实现接口幂等性校验
  8. Hive中元数据表的关系和如何在元数据中删除表
  9. jmeter性能测试用户参数参数化
  10. switch语句训练