題目如下: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];
}
}
//輸出map
for (map<int, int>::iterator iter = result.begin(); iter != result.end(); iter++)
{
cout << iter->first << ":" << iter->second << endl;
}
}
return 0;
}