PAT(Advanced)查找元素

2020-05-26 Views PAT1422字9 min read
featureimg

1011 World Cup Betting (20分)

Question

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.

For example, 3 games' odds are given as the following:

 W    T    L
1.1  2.5  1.7
1.2  3.1  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1×3.1×2.5×65%−1)×2=39.31 yuans (accurate up to 2 decimal places).

Input Specification

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output Specification

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input

1.1 2.5 1.7
1.2 3.1 1.6
4.1 1.2 1.1

Sample Output1

T T W 39.31

Analysis

这题题意是给出3场比赛的赢平输的赔率,找出每一场比赛赔率最大的W T L,最后计算出收益。

Code

#include <iostream>
#include <cstdio>
using namespace std;
char cmp(double g[]){
	if(g[0]>g[1]){
		if(g[0]>g[2])return 'W';
		else return 'L';
	}else{
		if(g[1]>g[2])return 'T';
		else return 'L';
	}
}
int main() {
	double game[3]={0.0};
	double ans=1.0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			scanf("%lf",&game[j]);
		}
		printf("%c ",cmp(game));
		ans*=max(max(game[0],game[1]),game[2]);
	}
	ans=(ans*0.65-1)*2;
	printf("%.2f",ans);
	return 0;
}

1006 Sign In and Sign Out (25分)

Question

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output

SC3021234 CS301133

Analysis

这题题意是给出n个人的id,sign in时间和 sign out时间,找出sign in时间最早的人和sign out最晚的人的id。一开始写了个结构体,包含id,sign in的时分秒,sign out的时分秒,写下来实在繁琐😓而且在min和max的赋初值上有点问题。因此参考柳神的思路,引入INT_MAX和INT_MIN,在循环里直接做判断。

Code

#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;

int main() {
	int m,min=INT_MAX,max=INT_MIN;
	string sin,sout;
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		string s;
		cin>>s;
		int in_hh,in_mm,in_ss,out_hh,out_mm,out_ss;
		scanf("%d:%d:%d %d:%d:%d",&in_hh,&in_mm,&in_ss,&out_hh,&out_mm,&out_ss);
		if((in_ss+in_mm*60+in_hh*3600)<=min){
			min=in_ss+in_mm*60+in_hh*3600;
			sin=s;
		}
		if((out_ss+out_mm*60+out_hh*3600)>=max){
			max=out_ss+out_mm*60+out_hh*3600;
			sout=s;
		}
	}
	cout<<sin<<" "<<sout;
	return 0;
}

1036 Boys vs Girls (25分)

Question

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF−gradeM. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.

Sample Input1

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output1

Mary EE990830
Joe Math990112
6

Sample Input2

1
Jean M AA980920 60

Sample Output2

Absent
Jean AA980920
NA

Analysis

这题题意是给出n个人的名字、性别、id、成绩,找出其中成绩最高的女生和成绩最低的男生,并输出姓名和id,以及两人的成绩差值。如果没有符合要求的男生或者女生,就输出Absent,成绩差值为NA。

这题也属于比较简单的题目,边输入边判断性别和成绩是否满足要求。注意最高成绩和最低成绩 的初值为-1和101。

Code

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
	int n,high=-1,low=101;
	string fn,fi,mn,mi;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		string name,id;
		char gender;
		int grade;
		cin>>name>>gender>>id>>grade;
		if(gender=='F' && grade>high){
			fn=name;fi=id;
			high=grade;
		}
		if(gender=='M'&&grade<low){
			mn=name;mi=id;
			low=grade;
		}
	}
	if(high==-1)cout<<"Absent"<<endl;
	else cout<<fn<<" "<<fi<<endl;
	if(low==101)cout<<"Absent"<<endl;
	else cout<<mn<<" "<<mi<<endl;
	if(high==-1||low==101)cout<<"NA";
	else cout<<high-low;
	return 0;
}
EOF