華爲機試 - 求字符串中最大最小值

輸入一串字符,只包含“0-10”和“,”找出其中最小的數字和最大的數字(可能不止一個),輸出最後剩餘數字個數。如 輸入 “3,3,4,5,6,7,7”

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



int main()
{
	map<char,int> strMap;
	string str;
	cin>>str;
	for(size_t i=0;i<str.size();i++)
	{
		strMap[str[i]]++;
	}
	map<char,int>::iterator iterMap=strMap.begin();
	if(iterMap->first==',')iterMap++;
	int minNum=iterMap->first-'0';
	int minCount=iterMap->second;
	iterMap=strMap.end();
	iterMap--;
	int maxNum=iterMap->first-'0';
	int maxCount=iterMap->second;
	cout<<minNum<<' '<<maxNum<<' '<<str.size()-minCount-maxCount<<endl;
	system("pause");
	return 0;
}


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