PAT (Basic Level) Practice (中文)1046-1047

1046 划拳 (15 分)

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的划拳记录,请你统计他们最后分别喝了多少杯酒。

输入格式:

输入第一行先给出一个正整数 N(≤100),随后 N 行,每行给出一轮划拳的记录,格式为:

甲喊 甲划 乙喊 乙划

其中是喊出的数字,是划出的数字,均为不超过 100 的正整数(两只手一起划)。

输出格式:

在一行中先后输出甲、乙两人喝酒的杯数,其间以一个空格分隔。

输入样例:

5
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15

输出样例:

1 2
#include<iostream>
using namespace std;
int main()
{
	int a,b,c,d;
	int N;
	cin>>N;
	int jia=0;
	int yi=0;
	while(N--)
	{
		cin>>a>>b>>c>>d;
		if(b==a+c)
		{
			if(d!=a+c)
				yi++;
		}
		else
		{
			if(d==a+c)
				jia++;
		}
	}
	cout<<jia<<" "<<yi;
	return 0;
}

1047 编程团体赛 (20 分)

编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。

现给定所有队员的比赛成绩,请你编写程序找出冠军队。

输入格式:

输入第一行给出一个正整数 N(≤10​4​​),即所有参赛队员总数。随后 N 行,每行给出一位队员的成绩,格式为:队伍编号-队员编号 成绩,其中队伍编号为 1 到 1000 的正整数,队员编号为 1 到 10 的正整数,成绩为 0 到 100 的整数。

输出格式:

在一行中输出冠军队的编号和总成绩,其间以一个空格分隔。注意:题目保证冠军队是唯一的。

输入样例:

6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61

输出样例:

11 176
#include<iostream>
#include<map>
#include<algorithm>
#include<set>
using namespace std;
typedef struct Team
{
	int number;
	int score;
}Team;
struct Rule
{
	bool operator()(const Team&T1,const Team&T2)
	{
		return T1.score>T2.score;
	}
};
int main()
{
	int N;
	cin>>N;

	map<int,int>mp;
	while(N--)
	{
		string str;
		cin>>str;
		int number=0;
		int score;
		cin>>score;
		for(int i=0;i<str.length();i++)
		{
			if(str[i]=='-')
				break;
			number*=10;
			number+=(str[i]-'0');
		}
		mp[number]+=score;
	}
	set<Team,Rule>st;
	for(map<int,int>::iterator i=mp.begin();i!=mp.end();i++)
	{
		Team T;
		T.number=i->first;
		T.score=i->second;
		st.insert(T);
	}
	set<Team,Rule>::iterator i=st.begin();
	Team T=*i;
	cout<<T.number<<" "<<T.score;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41066584/article/details/94594119