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];
			}
		}

		//輸出map
		for (map<int, int>::iterator iter = result.begin(); iter != result.end(); iter++)
		{
			cout << iter->first << ":" << iter->second << endl;
		}
	}
    return 0;
}

發佈了46 篇原創文章 · 獲贊 23 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章