HDU 1225(Football Score)

Using map data structure, required output can be sorted.

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 100000;

struct team //球队
{
	string name; //球队名
	int score; //得分
	int netGoal; //净进球数
	int totalGoal; //总踢入数
}t[MAXN];

//排序规则
bool cmp(team t1, team t2)
{
	if (t1.score != t2.score) //首先比得分
		return t1.score > t2.score;
	else if (t1.netGoal != t2.netGoal) //第二比净进球数
		return t1.netGoal > t2.netGoal;
	else if (t1.totalGoal != t2.totalGoal) //第三比总踢入数
		return t1.totalGoal > t2.totalGoal;
	else //最后比球队名
		return t1.name < t2.name;
}

int main()
{
	//取消cin、cout和stdio的同步,提高cin、cout速度
	std::ios::sync_with_stdio(false);
	int N;
	while (cin >> N)
	{
		for (int i = 0; i < N; i++) //初始化球队信息
		{
			t[i].score = t[i].netGoal = t[i].totalGoal = 0;
		}

		string p, vs, q; //球队名
		int ps, qs; //球队比分
		char o;
		map<string, int> ts; //球队名-得分
		map<string, int> tn; //球队名-净进球数
		map<string, int> tt; //球队名-总踢入数

		for (int i = 0; i < N * (N - 1); i++) //输入
		{
			cin >> p >> vs >> q >> ps >> o >> qs;
			tn[p] += (ps - qs);
			tn[q] += (qs - ps);
			tt[p] += ps;
			tt[q] += qs;
			if (ps > qs)
				ts[p] += 3;
			else if (ps < qs)
				ts[q] += 3;
			else
			{
				ts[p]++;
				ts[q]++;
			}
		}

		map<string, int>::iterator it1, it2, it3;
		int i = 0;
		//将队伍名,得分,净进球数,总踢入数存入结构体数组
		for (it1 = ts.begin(), it2 = tn.begin(), it3 = tt.begin();
			it1 != ts.end(), it2 != tn.end(), it3 != tt.end();
			it1++, it2++, it3++)
		{
			t[i].name = it1->first;
			t[i].score = it1->second;
			t[i].netGoal = it2->second;
			t[i].totalGoal = it3->second;
			i++;
		}

		sort(t, t + N, cmp); //排序
		for (int i = 0; i < N; i++) //输出
		{
			cout << t[i].name << " " << t[i].score << endl;
		}
		cout << endl;
	}
	return 0;
}

Keep up.

Published 152 original articles · won praise 1 · views 7624

Guess you like

Origin blog.csdn.net/Intelligence1028/article/details/104679727
Recommended