用map保存Word及次數(shù),由于map自動(dòng)按字典序保存,最后只要輸出第一個(gè)和max一樣的字符就可以了
#include<iostream>#include<string>#include<vector>#include<map>#include<cctype>#include<algorithm>#PRagma warning(disable:4996)using namespace std;int main(){ string str; getline(cin, str); map<string, int> all; string temp; int max = 0; for (auto x : str)//處理字符,并計(jì)算出最大出現(xiàn)次數(shù) { if ((x >= '0'&&x <= '9') ||//對(duì)于0-9,a-z,A-Z的字符增加在當(dāng)前temp末尾(小寫(xiě)) (x >= 'a'&&x <= 'z') || (x >= 'A'&&x <= 'Z')) temp += tolower(x); else if (!temp.empty())//對(duì)于非以上字符,判斷temp是否為空字符,若不為空,保存 { if (++all[temp] > max) max = all[temp]; temp=""; } } if(!temp.empty()) if (++all[temp] > max) max = all[temp];//處理最后剩余的字符 for (auto it = all.begin();it != all.end();it++)//輸出 if (it->second == max) { printf("%s %d/n", it->first.c_str(), it->second);exit(0); }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注