PAT 1136. A Delayed Palindrome (20)-PAT甲级真题

Consider a positive integer N written in standard notation with k+1 digits ai as ak…a1a0 with 0 <= ai < 10 for all i and ak > 0. Then N is palindromic if and only if ai = ak-i for all i. Zero is written 0 and is also palindromic by definition.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number)

Given any positive integer, you are supposed to find its paired palindromic number.

Input Specification:

Each input file contains one test case which gives a positive integer no more than 1000 digits.

Output Specification:

For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:

A + B = C
where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number — in this case we print in the last line “C is a palindromic number.”; or if a palindromic number cannot be found in 10 iterations, print “Not found in 10 iterations.” instead.

Sample Input 1:
97152
Sample Output 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
Sample Input 2:
196
Sample Output 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

分析:1 将字符串倒置与原字符串比较看是否相等可知s是否为回文串
2 字符串s和它的倒置t相加,只需从头到尾相加然后再倒置(记得要处理最后一个进位carry,如果有进位要在末尾+’1’)
3 倒置可采用algorithm头文件里面的函数reverse(s.begin(), s.end())直接对s进行倒置

 

1137. Final Grading (25)-PAT甲级真题

For a student taking the online course “Data Structures” on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. The final grade is calculated by G = (Gmid-termx 40% + Gfinalx 60%) if Gmid-term > Gfinal, or Gfinal will be taken as the final grade G. Here Gmid-term and Gfinal are the student’s scores of the mid-term and the final exams, respectively.

The problem is that different exams have different grading sheets. Your job is to write a program to merge all the grading sheets into one.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integers: P , the number of students having done the online programming assignments; M, the number of students on the mid-term list; and N, the number of students on the final exam list. All the numbers are no more than 10,000.

Then three blocks follow. The first block contains P online programming scores Gp’s; the second one contains M mid-term scores Gmid-term’s; and the last one contains N final exam scores Gfinal’s. Each score occupies a line with the format: StudentID Score, where StudentID is a string of no more than 20 English letters and digits, and Score is a nonnegative integer (the maximum score of the online programming is 900, and that of the mid-term and final exams is 100).

Output Specification:

For each case, print the list of students who are qualified for certificates. Each student occupies a line with the format:

StudentID Gp Gmid-term Gfinal G

If some score does not exist, output “-1” instead. The output must be sorted in descending order of their final grades (G must be rounded up to an integer). If there is a tie, output in ascending order of their StudentID’s. It is guaranteed that the StudentID’s are all distinct, and there is at least one qualified student.

Sample Input:
6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81
Sample Output:
missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84

分析:1 因为所有人必须要G编程>=200分,所以用v数组保存所有G编程>=200的人,(一开始gm和gf都为-1),用map映射保存名字所对应v中的下标(为了避免与“不存在”混淆,保存下标+1,当为0时表示该学生的姓名在v中不存在)
2 G期中中出现的名字,如果对应的map并不存在(==0),说明该学生编程成绩不满足条件,则无须保存该学生信息。将存在的人的期中考试成绩更新
3 G期末中出现的名字,也必须保证在map中存在。先更新G期末和G总为新的成绩,当G期末<G期中时再将G总更新为(G期中x 40% + G期末x 60%)
4 将v数组中所有G总满足条件的放入ans数组中,对ans排序(总分递减,总分相同则姓名递增),最后输出ans中的学生信息

 

1074. 宇宙无敌加法器(20)-PAT乙级真题

地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的。而在PAT星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”。每个PAT星人都必须熟记各位数字的进制表,例如“……0527”就表示最低位是7进制数、第2位是2进制数、第3位是5进制数、第4位是10进制数,等等。每一位的进制d或者是0(表示十进制)、或者是[2,9]区间内的整数。理论上这个进制表应该包含无穷多位数字,但从实际应用出发,PAT星人通常只需要记住前20位就够用了,以后各位默认为10进制。
在这样的数字系统中,即使是简单的加法运算也变得不简单。例如对应进制表“0527”,该如何计算“6203+415”呢?我们得首先计算最低位:3+5=8;因为最低位是7进制的,所以我们得到1和1个进位。第2位是:0+1+1(进位)=2;因为此位是2进制的,所以我们得到0和1个进位。第3位是:2+4+1(进位)=7;因为此位是5进制的,所以我们得到2和1个进位。第4位是:6+1(进位)=7;因为此位是10进制的,所以我们就得到7。最后我们得到:6203+415=7201。
输入格式:
输入首先在第一行给出一个N位的进制表(0 < N <=20),以回车结束。 随后两行,每行给出一个不超过N位的正的PAT数。
输出格式:
在一行中输出两个PAT数之和。
输入样例:
30527
06203
415
输出样例:
7201

分析:先将要相加的两个字符串S1和S2都扩展到和S等长,然后从后往前按照进制相加到ans中,注意进位carry,最后输出字符串ans,记得不要输出字符串ans前面的0。如果一次都没有输出,最后要输出一个0~

1073. 多选题常见计分法(20)-PAT乙级真题

批改多选题是比较麻烦的事情,有很多不同的计分方法。有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到50%分数;如果考生选择了任何一个错误的选项,则不能得分。本题就请你写个程序帮助老师批改多选题,并且指出哪道题的哪个选项错的人最多。
输入格式:
输入在第一行给出两个正整数N(<=1000)和M(<=100),分别是学生人数和多选题的个数。随后M行,每行顺次给出一道题的满分值(不超过5的正整数)、选项个数(不少于2且不超过5的正整数)、正确选项个数(不超过选项个数的正整数)、所有正确选项。注意每题的选项从小写英文字母a开始顺次排列。各项间以1个空格分隔。最后N行,每行给出一个学生的答题情况,其每题答案格式为“(选中的选项个数 选项1 ……)”,按题目顺序给出。注意:题目保证学生的答题情况是合法的,即不存在选中的选项数超过实际选项数的情况。
输出格式:
按照输入的顺序给出每个学生的得分,每个分数占一行,输出小数点后1位。最后输出错得最多的题目选项的信息,格式为:“错误次数 题目编号(题目按照输入的顺序从1开始编号)-选项号”。如果有并列,则每行一个选项,按题目编号递增顺序输出;再并列则按选项号递增顺序输出。行首尾不得有多余空格。如果所有题目都没有人错,则在最后一行输出“Too simple”。
输入样例1:
3 4
3 4 2 a c
2 5 1 b
5 3 2 b c
1 5 4 a b d e
(2 a c) (3 b d e) (2 a c) (3 a b e)
(2 a c) (1 b) (2 a b) (4 a b d e)
(2 b d) (1 e) (1 c) (4 a b c d)
输出样例1:
3.5
6.0
2.5
2 2-e
2 3-a
2 3-b
输入样例2:
2 2
3 4 2 a c
2 5 1 b
(2 a c) (1 b)
(2 a c) (1 b)
输出样例2:
5.0
5.0
Too simple

分析:错误是指错选或者漏选。用异或运算来判断一个选项和正确选项是否匹配,如果是匹配的,那么异或的结果应当是0;如果不匹配,那么这个选项就是存在错选或者漏选的情况~例如:设a为00001,b为00010,c为00100,d为01000,e为10000,如果给定的正确答案是ac,即10001,那么如果给出的选项也是10001,异或的结果就是0;如果给出的选项是a或者ab,即00001或00011,异或之后不为0,就是错选和漏选的~通过异或操作的结果,再用与运算就可以把错选和漏选的选项找出来~fullscore表示一道题满分的分值;trueopt表示正确的选项,存储的是正确选项二进制的值,二进制由hash给出分别是1,2,4,8,16;cnt是错误的次数,maxcnt是最大错误次数~

 

1072. 开学寄语(20)-PAT乙级真题

下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其QQ,封其电脑,夺其手机,收其ipad,断其wifi,使其百无聊赖,然后,净面、理发、整衣,然后思过、读书、锻炼、明智、开悟、精进。而后必成大器也!
本题要求你写个程序帮助这所学校的老师检查所有学生的物品,以助其成大器。
输入格式:
输入第一行给出两个正整数N(<= 1000)和M(<= 6),分别是学生人数和需要被查缴的物品种类数。第二行给出M个需要被查缴的物品编号,其中编号为4位数字。随后N行,每行给出一位学生的姓名缩写(由1-4个大写英文字母组成)、个人物品数量K(0 <= K <= 10)、以及K个物品的编号。
输出格式:
顺次检查每个学生携带的物品,如果有需要被查缴的物品存在,则按以下格式输出该生的信息和其需要被查缴的物品的信息(注意行末不得有多余空格):
姓名缩写: 物品编号1 物品编号2 ……
最后一行输出存在问题的学生的总人数和被查缴物品的总数。
输入样例:
4 2
2333 6666
CYLL 3 1234 2345 3456
U 4 9966 6666 8888 6666
GG 2 2333 7777
JJ 3 0012 6666 2333
输出样例:
U: 6666 6666
GG: 2333
JJ: 6666 2333
3 5

分析:bool类型的forbid存储禁止携带的物品,如果需要被查缴则赋值为true;flag变量表示当前学生是否已经输出过姓名,一开始flag=false,当前学生如果有需要被查缴的物品且还未输出过他的姓名,则输出name,并令flag=true;如果有需要被查缴的物品且已经输出过姓名,则输出该物品的编号,因为编号为4位数字,不满4位要在前面补0,所以用%04d输出,并将被查缴物品的总数fnum++,最后如果当前学生已经输出过姓名,则输出一个空行,并将学生的总人数snum++,最后输出snum和fnum的值~

 

1071. 小赌怡情(15)-PAT乙级真题

常言道“小赌怡情”。这是一个很简单的小游戏:首先由计算机给出第一个整数;然后玩家下注赌第二个整数将会比第一个数大还是小;玩家下注t个筹码后,计算机给出第二个数。若玩家猜对了,则系统奖励玩家t个筹码;否则扣除玩家t个筹码。
注意:玩家下注的筹码数不能超过自己帐户上拥有的筹码数。当玩家输光了全部筹码后,游戏就结束。

输入格式:

输入在第一行给出2个正整数T和K(<=100),分别是系统在初始状态下赠送给玩家的筹码数、以及需要处理的游戏次数。随后K行,每行对应一次游戏,顺序给出4个数字:
n1 b t n2
其中n1和n2是计算机先后给出的两个[0, 9]内的整数,保证两个数字不相等。b为0表示玩家赌“小”,为1表示玩家赌“大”。t表示玩家下注的筹码数,保证在整型范围内。

输出格式:

对每一次游戏,根据下列情况对应输出(其中t是玩家下注量,x是玩家当前持有的筹码量):
玩家赢,输出
Win t! Total = x.
玩家输,输出
Lose t. Total = x.
玩家下注超过持有的筹码量,输出
Not enough tokens. Total = x.
玩家输光后,输出
Game Over.
并结束程序。

输入样例1:
100 4
8 0 100 2
3 1 50 1
5 1 200 6
7 0 200 8

输出样例1:
Win 100! Total = 200.
Lose 50. Total = 150.
Not enough tokens. Total = 150.
Not enough tokens. Total = 150.

输入样例2:
100 4
8 0 100 2
3 1 200 1
5 1 200 6
7 0 200 8
输出样例2:
Win 100! Total = 200.
Lose 200. Total = 0.
Game Over.

分析:ans表示n1和n2真实的结果,如果n1 > n2,ans为0,表示应该赌小,否则ans = 1,表示玩家应该赌大。T表示当前玩家有的筹码数,如果T=0,表示玩家已经输光,输出Game Over;如果t > T,表示玩家下注超过持有的筹码量,输出Not enough tokens. Total = 当前的T,如果真实结果ans等于玩家猜的结果,表示玩家赢了,筹码都归玩家,T += t;如果ans不等于b,表示玩家输了,筹码要减去t~