算法笔记 上机训练实战指南 第3章 入门篇(1)--入门模拟 学习笔记 3.2 查找元素

PAT B1041 考试座位号 (15分)

每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。

输入格式:

输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号。其中准考证号由 16 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。

考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M 个待查询的试机座位号码,以空格分隔。

输出格式:

对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用 1 个空格分隔。

输入样例:

4
3310120150912233 2 4
3310120150912119 4 1
3310120150912126 1 3
3310120150912002 3 2
2
3 4

输出样例:

3310120150912002 2
3310120150912119 1
#include<cstdio>
struct student{
    long long id;
    int seat;
}student[1010];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        long long id;
        int tryseat;
        int seat;
        scanf("%lld%d%d",&id,&tryseat,&seat);
        student[tryseat].id = id;
        student[tryseat].seat = seat;
    }
    int m;
    scanf("%d",&m);
    while(m--){
        int temp;
        scanf("%d",&temp);
        printf("%lld %d
",student[temp].id,student[temp].seat);
    }
    return 0;
}
PAT B1004 成绩排名 (20分)

读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:

每个测试输入包含 1 个测试用例,格式为

1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

其中姓名学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:

对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112
#include<cstdio>
struct student{
    char name[15];
    char id[15];
    int score;
}temp,ans_min,ans_max;
int main(){
    int n;
    scanf("%d",&n);
    ans_min.score = 101;
    ans_max.score = -1;
    while(n--){
        scanf("%s%s%d",temp.name,temp.id,&temp.score);
        if(temp.score > ans_max.score){
            ans_max = temp;
        }
        if(temp.score < ans_min.score){
            ans_min = temp;
        }
    }
    printf("%s %s
",ans_max.name,ans_max.id);
    printf("%s %s
",ans_min.name,ans_min.id);
    return 0;
}
PAT B1028 人口普查 (20分)

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数 N,取值在(;随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入样例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

输出样例:

3 Tom John
#include<cstdio>
struct person{
    char name[10];
    int year;
    int month;
    int day;
}temp,youngest,oldest,left,right;
bool EquLess(person a,person b){
    if(a.year != b.year) return a.year <= b.year;
    else if (a.month != b.month) return a.month <= b.month;
    else return a.day <= b.day;
}
bool EquMore(person a,person b){
    if(a.year != b.year) return a.year >= b.year;
    else if(a.month != b.month) return a.month >= b.month;
    else return a.day >= b.day;
}
void init(){
    oldest.year = right.year = 2014;
    oldest.month = youngest.month = left.month = right.month =  9;
    oldest.day = youngest.day = left.day = right.day =  6;
    youngest.year = left.year = 1814;
}
int main(){
    init();
    int n,num=0;
    scanf("%d",&n);
    while(n--){
        scanf("%s %d/%d/%d",temp.name,&temp.year,&temp.month,&temp.day);
        if(EquLess(temp,right) && EquMore(temp,left)){
            num++;
            if(EquLess(temp,oldest)) 
                oldest = temp;
            if(EquMore(temp,youngest)) 
                youngest = temp;
        }
    }
    if(num == 0)
        printf("0
");
    else
        printf("%d %s %s
",num,oldest.name,youngest.name);
    return 0;
}
PAT B1032 挖掘机技术哪家强 (20分)

为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

输入格式:

输入在第 1 行给出不超过 1 的正整数 N,即参赛人数。随后 N 行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从 1 开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

输出格式:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

输入样例:

6
3 65
2 80
1 100
2 70
3 40
3 0

输出样例:

2 150
#include<cstdio>
const int maxn = 100010;
int main(){
    int n;
    scanf("%d",&n);
    int school[maxn]={0};
    int schid,schscore;
    for(int i=0;i<n;i++){
        scanf("%d %d",&schid,&schscore);
        school[schid] = school[schid] + schscore;
    }
    int maxid,maxscore=-1;
    for(int i=1;i<=n;i++){
        if(school[i]>maxscore){
            maxscore = school[i];
            maxid = i;
        }
    }
    printf("%d %d
",maxid,maxscore);
    return 0;
}
PAT A1011 World Cup Betting (20分)

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 ( 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 WT 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
#include<cstdio>
int main(){
    double a;
    char s[3]={'W','T','L'};
    double ans=1;
    int id[3];
    double score[3][3];
    for(int i=0;i<3;i++){
        double temp = 0;
        for(int j=0;j<3;j++){
            scanf("%lf",&a);
            if(a >temp){
                temp = a;
                id[i] = j;
            }
        }
        ans = ans * temp;
    }
    ans = (ans*0.65 -1)*2;
    printf("%c %c %c %.2f",s[id[0]],s[id[1]],s[id[2]],ans);
    return 0;
}
PAT A1006 Sign In and Sign Out (25分)

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
#include<cstdio>
struct pNode{
    char name[20];
    int hh;
    int mm;
    int ss;
}temp,ans1,ans2;
bool great(pNode a,pNode b){
    if(a.hh != b.hh)     return a.hh > b.hh;
    else if(a.mm != b.mm) return a.mm > b.mm;
    else if(a.ss != b.ss) return a.ss > b.ss;
}
int main(){
    int n;
    ans1.hh = 24,ans1.mm=60,ans1.ss=60;
    ans2.hh = 0,ans2.mm = 0,ans2.ss=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s %d:%d:%d",temp.name,&temp.hh,&temp.mm,&temp.ss);
        if(great(temp,ans1) == false) ans1 = temp;
        scanf("%d:%d:%d",&temp.hh,&temp.mm,&temp.ss);
        if(great(temp,ans2) == true) ans2 = temp;
    }
    printf("%s %s",ans1.name,ans2.name);
    return 0;
}
PAT A1036 Boys vs Girls (25分)

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 namegenderID 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 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
#include<cstdio>
struct student{
    char name[15];
    char gender;
    char id[15];
    int score;
}male,female,temp;
int main(){
    int n;
    scanf("%d",&n);
    female.score = -1;
    male.score = 101;
    int mnum=0,fnum=0;//分别记录男生和女生的数量 
    for(int i=0;i<n;i++){
        scanf("%s %c %s %d",temp.name,&temp.gender,temp.id,&temp.score);
        if(temp.gender=='M'){
            mnum++;
            if(temp.score < male.score){
                male = temp;
            }
        }else if(temp.gender=='F'){
            fnum++;
            if(temp.score > female.score){
                female = temp;
            }
        }
    }
    
    if(fnum==0){
        printf("Absent
");
    }else{
        printf("%s %s
",female.name,female.id);
    }
    
    if(mnum==0){
        printf("Absent
");
    }else{
        printf("%s %s
",male.name,male.id);
    }
    
    if(mnum==0 || fnum == 0){
        printf("NA
");
    }else{
        printf("%d
",female.score-male.score);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/coderying/p/12207846.html