2014年2月24日 星期一

UVa OJ - 494 Kindergarten Counting Game

The following program is my ACcepted code for UVA-494.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UAV 494 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


//This program is for UVA 494 Kindergarten Counting Game
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=435

#include<iostream>
#include<stdio.h>
#include<string.h>
#define MAX 500
using namespace std;
/*  check whether the character c is a letter. (A~Z and a~z)
    return true or false for the judgement
*/
bool checkChar(char c)
{
    if(c >= 'a' && c <= 'z')
        return true;
    if(c >= 'A' && c <= 'Z')
        return true;  
    return false;
}

int main()
{
    char s[MAX];
    int answer = 0;
 
    /*  Variable waitNewWord is boolean
        whether now it is counting the old word -> false
        or it is waiting for the next new word  -> true
    */
    bool waitNewWord = true;
 
    while(gets(s)!=NULL)
    {
        waitNewWord = true;
        answer = 0;
     
        int len = strlen(s);
     
        for(int i= 0; i<len; i++)
        {
            if(waitNewWord)
            {
                if( checkChar(s[i]) )//wait new world and face a letter
                {
                    waitNewWord = false;
                    answer ++;//add one words.
                    continue;
                }
                else//contine waiting
                    continue;
            }
            else//the waitNewWorld is false (counting the old word)
            {
                if( checkChar(s[i]) )//contine counting the old word
                    continue;
                else//face the end of word
                {
                    waitNewWord = true;
                    continue;
                }
            }
        }
        printf("%d\n", answer);
    }
 
    return 0;
}

Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!