HDU 1563(Find your present!)

基礎題,使用 map 數據結構即可。

#include <iostream>
#include <map>
using namespace std;

map<int, int> mp;

int main()
{
	int n;
	while (cin >> n)
	{
		if (n == 0)
			break;

		mp.clear();
		int num;
		while (n--) //輸入,記錄每個輸入數的個數
		{
			cin >> num;
			if (mp.find(num) != mp.end()) //該輸入已存在,個數加1
				++mp[num];
			else //該輸入不存在,即首次輸入,個數爲1
				mp[num] = 1;
		}

		map<int, int>::iterator it;
		for (it = mp.begin(); it != mp.end(); it++) //遍歷map
		{
			if (it->second == 1) //個數爲1的輸入,輸出即可
			{
				cout << it->first << endl;
				break;
			}
		}
	}
	return 0;
}

繼續加油。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章