本文主要是介绍PAT B1021 -- 个位数统计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目如下:LINK
代码如下:
#include <iostream>
#include <string>
#include <map>using namespace std;int main()
{string strN;int count[10] = {0,0,0,0,0,0,0,0,0,0};while (cin >> strN){//计算每个个位数的个数for (int i = 0; i < strN.size(); i++){for (int j = 0; j < 10; j++){if (strN[i] - 48 == j){count[j]++;break;}}}//将结果存储到map中map<int, int> result;for (int m = 0; m < 10; m++){if (count[m] != 0){result[m] = count[m];}}//输出mapfor (map<int, int>::iterator iter = result.begin(); iter != result.end(); iter++){cout << iter->first << ":" << iter->second << endl;}}return 0;
}
这篇关于PAT B1021 -- 个位数统计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!