[PAT解题报告] 个位数统计 (15)
转载from http://tech-wonderland.net/blog/pat-basic-level-practise-1021-1025-solutions.html
解题思路: 水题, 利用hash表统计每个数字的个数即可. 下面是AC代码:
#include <map> #include <iostream> #include <string> int main() { std::string strInput; std::cin >> strInput; std::map<char, int> mapCounts; int iLen = strInput.size(); for(int i = 0; i < iLen; ++i) { if(mapCounts.find(strInput[i]) == mapCounts.end()) mapCounts[strInput[i]] = 1; else ++mapCounts[strInput[i]] ;; } for(auto it = mapCounts.begin(); it != mapCounts.end(); ++it) { std::cout << it->first << ':' << it->second << std::endl; } return 0; }