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

解题

  1. 题目要求比较简单,重点任务是存下输入数据,找最小进入时间和最大离开时间。
  2. 对于时间的比较,要么直接比较字符串,要么转换成时分秒的数字类型再比较。显然,直接比较字符串是最快捷的选择。
  3. 对于输入数据的存储,
  • 可以设计一个简单类,分别对进入和离开时间排序,得到输出。
  • 也可以设置三个数组,以下标相对应。但需要自行遍历,记录最早进入和最晚离开人员。

源码

github link

A1008 Elevator (20) 简单模拟

原题

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:
3 2 3 1
Sample Output:
41

解题

  1. 简单模拟题目
  2. 输入不指定数量,需要读取完一行,注意这一点的处理 这一点正是第一遍做错的原因
  3. 主要任务是记录当前楼层,和目标楼层作差,以判断up和down,并正确计算时间。
  4. 主要错误是题意没理解,原题说明了

Each case contains a positive integer N, followed by N positive numbers 会指明给出多少个数字。

但是看题的时候根据输入样例就自行猜测这是不指定输入个数的格式。

源码

github link

A1011 World Cup Betting (20 分)

原题

With the 2010 FIFA WoArld 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 Lfor 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

解题

1.前摇过长,核心要求就是三组数据,每组三个,每组要找最大,且记录对应W?T?L?,最后通过公式计算获利,并保留两位小数
2.对于每组找最大,且记录WTL的要求,可以选择用map或者class或者vector<double>

  • map<double,char>不能应对同赔率情况,map<char,double>的排序相对麻烦,需要转成vector<pair<char,double>>再对值排序
  • vector<double>需要自行处理排序问题。
  • 相比之下用class最为方便,排序函数也容易写。

源码

github link

1036 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 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​ − 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

解题

  1. 因为要找男生最低分和女生最高分,所以分开存储,再排序。每个学生用class存储,分性别存在vector中。
  2. 对于某一性别学生记录不存在的判定,可以另设flag,或者用vector.size() == 0来判定
  3. 最后要输出学生信息,可以采用常规输出,或者重载运算符。为了练习,此次采用重载运算符的方法。
  4. 错误有两处
  • 类的构造函数自己重写后,针对后面的实例构造情况都要重新写。
  • v.begin()是iterator类型,要用*取指针目标才能赋值给元素类型变量。

源码

github link

A1042 Shuffling Machine (20’)

原题

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, …, S13,
H1, H2, …, H13,
C1, C2, …, C13,
D1, D2, …, D13,
J1, J2
where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:
2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
Sample Output:
S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

解题

  1. 核心要求就是分别给出54个元素和对应位置,按升序排列,并且不止一次。
  2. 元素的存储首先想到的就是map<int, string>,每次取同位置上的数字和牌,放入map中就自然有序。每次排列就取当前的牌面序列和指定下标序列重新生成map。另一个方法是class存储,每次排列就重写index成员,然后排列。相比之下,后一种方法更好。为了练习,分别试写。
  3. 第一种方法很常规,流程基本就是获取数据,填充index,排序
  4. 第二种方法因为map用的不多,在遍历map的时候遇到一点小问题。同时,auto这个关键字需要充分了解后才能熟练使用,不然不知道auto到底是什么类型

源码

github_link_1
github_link_2

1046 Shortest Distance (20 分)

原题

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N (in [ 3 , 10 ​ 5 ​ ] [3,10​^5​ ] [3,105]), followed by N integer distances D​1​​ D2​​ ⋯ D​N​​ , where D​i​​ is the distance between the i-th and the (i+1)-st exits, and D​N​​ is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M ( ≤ 10 ​ 4 ​ ​ ) M (≤10​^4​​ ) M(104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 1 0 ​ 7 ​ ​ 10^​7​​ 107.
Output Specification:
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3
10
7

解题

  1. 题目要求简单环形路径上任意两点间的最短距离。
  2. 考虑到路径是抽象上的环形,实际以线性序列存储两点间距离,对于给定的任意两点,应先调整成先小后大的顺序,依次累加得到distance1,再用环形总长-distance1得到distance2,取两个距离小的即可。
  3. 始终报超时错误。做了如下修改都没能解决问题
  • 改用c的io方式
  • 改vector为原始数组
  • 修改计算方法,确保累加次数最少

源码

1065 A+B and C (64bit) (20 分)

原题

Given three integers A, B and C in [ − 2 ​ 6 3 ​ ​ , 2 ​ 6 3 ​ ​ ] [−2​^63​​,2​^63​​] [263,263], you are supposed to tell whether A+B>C.
Input Specification:
The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.

Output Specification:
For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where X is the case number (starting from 1).

Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false

解题

  1. 题目要求很简单,按照输入数据的范围要求取longlong,longdouble都可以

源代码

1124 Raffle for Weibo Followers (20 分)

原题

John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo – that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.
Input Specification:
Each input file contains one test case. For each case, the first line gives three positive integers M (≤ 1000), N and S, being the total number of forwards, the skip number of winners, and the index of the first winner (the indices start from 1). Then M lines follow, each gives the nickname (a nonempty string of no more than 20 characters, with no white space or return) of a follower who has forwarded John’s post.
Note: it is possible that someone would forward more than once, but no one can win more than once. Hence if the current candidate of a winner has won before, we must skip him/her and consider the next one.
Output Specification:
For each case, print the list of winners in the same order as in the input, each nickname occupies a line. If there is no winner yet, print Keep going… instead.
Sample Input 1:
9 3 2
Imgonnawin!
PickMe
PickMeMeMeee
LookHere
Imgonnawin!
TryAgainAgain
TryAgainAgain
Imgonnawin!
TryAgainAgain
Sample Output 1:
PickMe
Imgonnawin!
TryAgainAgain
Sample Input 2:
2 3 5
Imgonnawin!
PickMe
Sample Output 2:
Keep going…

解题

  1. 题目要求很简单,存入数据到vector<string>,遍历过程中按距离取元素,有重复就取下一个元素

源码

github link

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值