題目敘述
題目採EOF方式收資料,每筆資料有一行字串,當字串是'#'時停止收資料。判斷該字串的語言為何 (詳情請見範例測資)。
範例輸入
HELLO
HOLA
HALLO
BONJOUR
CIAO
ZDRAVSTVUJTE
#
範例輸出
Case 1: ENGLISH
Case 2: SPANISH
Case 3: GERMAN
Case 4: FRENCH
Case 5: ITALIAN
Case 6: RUSSIAN
解題思路
使用if來判斷該字串的語言為何,可以使用一個int變數放在EOF外面來確認目前是第幾個Case。
解題程式碼如下 (僅供參考):
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int count = 1;
string str;
while (cin >> str)
{
if (str == "#") break;
else
{
if (str == "HELLO") cout << "Case " << count << ": ENGLISH" << endl;
else if (str == "HOLA") cout << "Case " << count << ": SPANISH" << endl;
else if (str == "HALLO") cout << "Case " << count << ": GERMAN" << endl;
else if (str == "BONJOUR") cout << "Case " << count << ": FRENCH" << endl;
else if (str == "CIAO") cout << "Case " << count << ": ITALIAN" << endl;
else if (str == "ZDRAVSTVUJTE") cout << "Case " << count << ": RUSSIAN" << endl;
else cout << "Case " << count << ": UNKNOWN" << endl;
count++;
}
}
}
留言
張貼留言