HDU1263,水果(STL)

用map操作
代码如下:

#include<iostream>
#include<map>
#include<set>
#include<string>
using namespace std;
struct fruit
{
	string name;
	string place;
	bool operator < (const fruit& b) const
	{
		if(b.place!=place)
			return b.place>place;	//这里要“>"才能按字典序输出 
		return b.name>name;
	}
};
map<fruit,int> mp;	//map的key要用成结构体,这样才能满足先按地名字典序输出,再按水果名字典序输出的要求。
set<string> st;

void read()
{
	int t;
	fruit p;
	cin>>p.name>>p.place>>t;
	if(mp.empty())
		mp.insert(pair<fruit,int>(p,t));
	else
	{
		int flag=0;
		map<fruit,int>::iterator it;
		it=mp.find(p);
		if(it==mp.end())
			mp.insert(pair<fruit,int>(p,t));
		else
			(*it).second+=t;
	}
	st.insert(p.place);
}

void print()
{
	map<fruit,int>::iterator i=mp.begin();
	for(set<string>::iterator it=st.begin();it!=st.end();++it)
	{
		string t=*it;
		cout<<t<<endl;
		while(i!=mp.end()&&(*i).first.place==t)
		{
			cout<<"   |----"<<(*i).first.name<<'('<<(*i).second<<')'<<endl;
			++i;
		}
	}
}

int main()
{
	int T,M;
	cin>>T;
	while(T--)
	{
		cin>>M;
		while(M--)
			read();
		print();
		mp.clear();
		st.clear();
		if(T)	cout<<endl;
	}
	return 0;
}
上一篇:利用python+tkinter开发一个点名软件


下一篇:pytorch小知识点(一)-------in-place operation