2018年3月15日 星期四

[C_DT60-易] 高頻字元(C++)

[C_DT60-易] 高頻字元

Time Limit: 2 seconds
問題描述 :
從字串 str 中找出出現頻率最高的字元,所有資料中,最高頻的字元被設計成唯一。 str 中可能包含大小寫英文字母、數字、空白、標點符號等。
輸入說明 :
第一行輸入一整數n(1<=n<=10),表示有幾筆測資。
接下來n行字串 str ,含各種大小寫與標點符號。
輸出說明 :
出現頻率最高的字元。 ( 不會有多個答案 )
範例 :

輸入範例輸出範例
We’re students!
Yes!!! ALL PASS!!!
e

  1. #include <iostream>    
  2. #include <map>    
  3. using namespace std;    
  4.      
  5. int main() {    
  6.         
  7.         string word;    
  8.         getline(cin, word);    
  9.         map<charint> count;    
  10.         for(int j = 0;j < word.size();j++)    
  11.         {    
  12.             if (count.find(word[j]) == count.end())    
  13.             {    
  14.                 count.insert(make_pair(word[j], 1));    
  15.             }    
  16.             else    
  17.             {    
  18.                 count[word[j]] = count[word[j]]+1;    
  19.             }    
  20.         }    
  21.         int max = -1;    
  22.         char ans;    
  23.         for(map<charint>::iterator iter = count.begin(); iter != count.end(); iter++)    
  24.         {    
  25.             if(iter->second > max)    
  26.             {    
  27.                 ans = iter->first;    
  28.                 max = iter->second;    
  29.             }    
  30.         }    
  31.         cout << ans << endl;    
  32.      
  33.     return 0;    
  34. }  

沒有留言:

張貼留言