1153

cin cout 输入输出会超时  用scanf() printf()输入输出

string类的输入输出方式

    scanf(): string.resize(length);
                 scanf("%s", &string[0]);

    printf(): printf("%s", string.c_str());

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
class Stu
{
public:
	string rcn;
	int score;
	string level;//1
	string city;//3
	string date;//6
	string testnum;//3
	void Assign()
	{
		level = rcn.substr(0, 1);
		city = rcn.substr(1, 3);
		date = rcn.substr(4, 6);
		testnum = rcn.substr(10, 3);
	}
};
struct Node3
{
	string city;
	int num;
};
struct Node2
{
	int num;
	int sum;
};
void Find1(vector<Stu> &S, string que);
void Find2(map<string, Node2> &S, string que);
void Find3(map<string, map<string, int>> &S, string que);
bool cmp1(Stu &A, Stu &B)
{
	if (A.score == B.score)
		return A.rcn < B.rcn;
	else
		return A.score > B.score;
}
bool  cmp2(Node3 &A, Node3 &B)
{
	if (A.num == B.num)
		return A.city < B.city;
	else
		return A.num > B.num;
}
int main()
{
	vector<Stu> S[5];//1T 2A 3B
	map<string, Node2> S2;//city 人数 分数
	map<string, map<string, int>> S3;//time 城市人数
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		Stu s;
		s.rcn.resize(13);
		scanf("%s %d", &s.rcn[0], &s.score);
		s.Assign();

		if (s.level == "T")
			S[1].push_back(s);
		else if (s.level == "A")
			S[2].push_back(s);
		else if (s.level == "B")
			S[3].push_back(s);

		if (S2.count(s.city) == 0)
		{
			S2[s.city].num = 1;
			S2[s.city].sum = s.score;
		}
		else
		{
			S2[s.city].num++;
			S2[s.city].sum += s.score;
		}
		

		if (S3[s.date].count(s.city) == 0)
			S3[s.date][s.city] = 1;
		else
			S3[s.date][s.city]++;
	}
	
	sort(S[1].begin(), S[1].end(), cmp1);
	sort(S[2].begin(), S[2].end(), cmp1);
	sort(S[3].begin(), S[3].end(), cmp1);

	int t = 1;
	while (t<=m)
	{
		int c;
		string que;
		char ques[20];

		//cin >> c >> que;

		scanf("%d %s", &c, ques);
		que = ques;

		cout << "Case " << t << ": " << c << " " << que << endl;
		switch (c)
		{
		case 1: 
		{
			if (que == "T")
			{
				Find1(S[1], que); break;
			}
			else if (que == "A")
			{
				Find1(S[2], que); break;
			}
			else if (que == "B")
			{
				Find1(S[3], que); break;
			}
		}
		case 2: Find2(S2, que); break;
		case 3: Find3(S3, que); break;
		}
		t++;
	}
	system("pause");
	return 0;
}
void Find1(vector<Stu> &S, string que)
{
	if (S.empty())
		cout << "NA" << endl;
	else
	{
		for (auto iter = S.begin(); iter != S.end(); iter++)
			//cout << iter->rcn << " " << iter->score << endl;
			printf("%s %d\n", iter->rcn.c_str(), iter->score);
	}
}
void Find2(map<string, Node2> &S, string que)
{

	int num = S[que].num;
	int sum = S[que].sum;
	if (num == 0)
		cout << "NA" << endl;
	else
		cout << num << ' ' << sum << endl;
	
}
void Find3(map<string, map<string, int>> &S, string que)
{
	
	vector<Node3> ans;
	for (const auto&m : S[que])
	{
		Node3 node;
		node.city = m.first;
		node.num = m.second;
		ans.push_back(node);
	}
	sort(ans.begin(), ans.end(), cmp2);
	if (ans.empty())
		cout << "NA" << endl;
	else
	{
		for (auto iter = ans.begin(); iter != ans.end(); iter++)
			//cout << iter->city << " " << iter->num << endl;
			printf("%s %d\n", iter->city.c_str(), iter->num);
	}
}
发布了195 篇原创文章 · 获赞 9 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/88049245