算法笔记3.2----查找元素
A1011 World Cup Betting
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 Output:
T T W 39.31
题⽬⼤意:给出三场⽐赛以及每场⽐赛的W、T、L的赔率,选取每⼀场⽐赛中赔率最⼤的三个数a b c,先输出三⾏各⾃选择的是W、T、L中的哪⼀个,然后根据计算公式 (a * b * c * 0.65 – 1) * 2 得出最⼤收益~
分析:以三个数⼀组的形式读取,读取完⼀组后输出最⼤值代表的字⺟,然后同时ans累乘该最⼤值,最后根据公式输出结果~
#include<cstdio>
using namespace std;
int main(){
char c[4] = {"WTL"};
double ans = 1.0;
for(int i=0;i<3;i++){
double maxvalue = 0.0;
int maxchar = 0;
for(int j=0;j<3;j++){
double temp;
scanf("%lf",&temp);
if(maxvalue <= temp){
maxvalue = temp;
maxchar = j;
}
}
ans *= maxvalue;
printf("%c ",c[maxchar]);
}
printf("%.2f",(ans*0.65-1)*2);
return 0;
}
A1006 Sign In and Sign Out
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
题⽬⼤意:给出n个⼈的id、sign in时间、sign out时间,求最早进来的⼈和最早出去的⼈的ID~
分析:将时间都转换为总秒数,最早和最迟的时间保存在变量minn和maxn中,并同时保存当前最早和最迟的⼈的ID,最后输出~
#include<iostream>
#include<climits>
using namespace std;
int main(){
int n, minn = INT_MAX, maxn = INT_MIN;
string unlocked,locked;
scanf("%d",&n);
for(int i=0; i < n; i++){
string t;
cin>> t;
int h1, m1, s1, h2, m2, s2;
scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
int tempIn = h1*3600 + m1*60 + s1;
int tempOut = h2*3600 + m2*60 + s2;
if(tempIn<minn){
minn = tempIn;
unlocked = t;
}
if(tempOut>maxn){
maxn = tempOut;
locked = t;
}
}
cout<< unlocked << " " << locked;
return 0;
}
A1036 Boys vs Girls
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 grade F −grade M . If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.
Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
Sample Output 1:
Mary EE990830
Joe Math990112
6
Sample Input 2:
1
Jean M AA980920 60
Sample Output 2:
Absent
Jean AA980920
NA
题⽬⼤意:给出N个同学的信息,输出⼥⽣中的最⾼分获得者的信息与男⽣中最低分获得者的信息,
并输出他们的分数差。如果不存在⼥⽣或者男⽣,则对应获得者信息处输出Absent,⽽且差值处输出
NA。
分析:⽤string类型的female和male保存要求的学⽣的信息,femalescore和malescore处保存男⽣的最低分和⼥⽣的最⾼分
⼀开始设femalescore为最低值-1,malescore为最⾼值101,最后根据分值是否为-1或者101来判断是否有相应的⼥⽣或者男⽣
#include<iostream>
using namespace std;
int main(){
int n;
scanf("%d",&n);
string male,female;
int femalescore = -1,malescore = 101;
for(int i = 0; i < n; i++){
string name,gender,num;
int score;
cin>>name>>gender>>num;
scanf("%d", &score);
if(gender=="F"){
if(score>femalescore){
femalescore = score;
female = name + " " + num;
}
}else if(score<malescore){
malescore = score;
male = name + " " +num;
}
}
if(femalescore != -1)
cout<<female<<endl;
else
printf("Absent\n");
if(malescore != 101)
cout<<male<<endl;
else
printf("Absent\n");
if(femalescore != -1&& malescore != 101)
printf("%d",femalescore - malescore);
else
printf("NA");
return 0;
}
关于climits头文件(A1036中使用)
INT_MAX ------ int的最大值
INT_MIN ------ int 的最小值
作用:当我们要求最大值的时候需要将最大值初始化为最小值,反过来要求最小值的时候会先初始化为最大值。