# 【Uva 解題筆記】494 - Kindergarten Counting Game Uva Online Judge:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=435 Zerojudge:https://zerojudge.tw/ShowProblem?problemid=a011 PDF Source:https://onlinejudge.org/external/4/494.pdf 題目翻譯: 每個人圍成一圈坐下。好的,仔細聽我說。 "Woooooo, you screwy wabbit!" 現在,能有人告訴我我剛說了多少個字嗎? **Input** 程式的輸入將由一系列的行組成,每一行包含多個單字(至少一個)。一個「單字」的定義是連續的字母序列(大寫和/或小寫字母)。 **Output** 你的程式應該要輸出每一行輸入的單字數量。每個單字計數應該印在單獨的一行上。 **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 ``` 解題思路: 建立一個 `bool inWord = false;` 變數,用於判斷是否為一個單字。 題目的正式測資可能會是 `Hello123world` ,這時候用 `isalpha(ch)` 去判斷是否為英文字母,不然可能會誤判成是一個單字。 範例程式碼: ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ string line; while (getline(cin, line)){ int count = 0; bool inWord = false; for (char ch : line){ if (isalpha(ch)){ if (!inWord){ inWord = true; } } else{ if (inWord){ count++; inWord = false; } } } if (inWord) count++; cout << count << "\n"; } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up