UVA494 Kindergarten Counting Game

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

问题链接UVA494 Kindergarten Counting Game

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

问题分析:实现方法有多种。可以用C语言的字符串函数strtok()来实现,也可以用字符流来实现。

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

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;
}
时间: 2024-10-04 00:25:04

UVA494 Kindergarten Counting Game的相关文章

494 - Kindergarten Counting Game

 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,

UVA 494 Kindergarten Counting Game

题目大意:输入一个字字符串,输出该字符串中所包含的"word"个数,其中"word"是指连续的字母(大小写均可) 题目思路:其实这是道水题,不过我考虑的时候,太想当然了,我是把空格作为每个子串的分界,遇到一个空格就去判断空格前的子串是否为单词. 然而实际上并不是这样,如果是连续的符号中夹杂着单词的话,就不好判断. 正确思路:对每个输入的字符进行判断,每当遇到非字母的字符时,进行单词判断. (有两点注意:1.输入为回车时,应输出结果并且初始化.2.输入为EOF(-1)

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up