2018年3月9日 星期五

[C_ST104-易] 單字音節(C++)

[C_ST104-易] 單字音節

Time Limit: 2 seconds
問題描述 :
給定一個單字字串 str( 由小寫字母組成 ) ,請計算此單字共有幾個音節並且輸出,音節的定義為母音的個數,但連續的母音只能算一個音節。
母音為 :a,e,i,o,u.
輸入說明 :
一個單字字串 str(1 ≤ 字串長度 ≤ 20) 。
輸出說明 :
將此單字之音節數輸出。
範例 :

輸入範例輸出範例
apple
public
out
print
see
static
2
2
1
1
1
2


  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     // [C_ST104-易] 單字音節  
  6.     string word;  
  7.     while(getline(cin, word))  
  8.     {  
  9.         int count = 0;  
  10.         for(int i = 0;i < word.size();i++)  
  11.         {  
  12.             if(i != 0)  
  13.             {  
  14.                 if((word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u') && (word[i-1] != 'a' && word[i-1] != 'e' && word[i-1] != 'i' && word[i-1] != 'o' && word[i-1] != 'u'))  
  15.                 {  
  16.                     count++;  
  17.                 }  
  18.             }  
  19.             else  
  20.             {  
  21.                 if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u')  
  22.                 {  
  23.                     count++;  
  24.                 }  
  25.             }  
  26.         }  
  27.         cout << count << endl;  
  28.     }  
  29.     return 0;  
  30. }  

沒有留言:

張貼留言