pat 甲级 1023-1026

1023. Have Fun with Numbers (20)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input file contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

Sample Input:
1234567899
Sample Output:
Yes
2469135798

代码:

origin = input()
double = str(int(origin) * 2)
if len(origin) != len(double):
    print('No')
else:
    notsame = 0
    dic_ori = {}
    for i in origin:
        dic_ori[i] = dic_ori.get(i, 0) + 1
    dic_dou = {}
    for i in double:
        dic_dou[i] = dic_dou.get(i, 0) + 1
    # print(dic_ori)
    for key, value in dic_ori.items():
        if key not in dic_dou.keys() or value != dic_dou[key]:
            notsame = 1
            break
    if notsame == 0:
        print('Yes')
    else:
        print('No')
print(double)

1024. Palindromic Number (25)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

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. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3

代码:

line = input().split()
N = int(line[0])
maxStep = int(line[1])
step = 0
while str(N)[::-1] != str(N):
    N = N + int(str(N)[::-1])
    step += 1
    if step >= maxStep:
        break
print(N)
print(step)

1025. PAT Ranking (25)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (<=100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (<=300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

代码:

N = int(input())
local = []
localrank = []
totalTestee = 0
location = {}

def rank(scoreDict):
    dic = sorted(scoreDict.items(), key = lambda x: x[1], reverse = True)
    rankdic = {}
    rank = 1
    score = 101
    for i in range(len(dic)):
        if dic[i][1] < score:
            rankdic[dic[i][0]] = i + 1
        else:
            rankdic[dic[i][0]] = rank
        score = dic[i][1]
        rank = rankdic[dic[i][0]]
    return rankdic

for i in range(N):
    local.append({})
    localrank.append({})
    localTestee = int(input())
    totalTestee += localTestee
    for j in range(localTestee):
        temp = input().split()
        location[temp[0]] = i
        local[i][temp[0]] = int(temp[1])
    localrank[i] = rank(local[i])
# print(local)
# print(localrank)

allTestee = {}
for i in range(N):
    allTestee = dict(allTestee, **local[i])
allTestee = rank(allTestee)
allTestee = sorted(allTestee.items(), key = lambda x: (x[1], x[0]))
# print(allTestee)
print(totalTestee)
for testee in allTestee:
    res = testee[0] + ' ' + str(testee[1]) + ' ' + str(location[testee[0]] + 1) + ' ' + str(localrank[location[testee[0]]][testee[0]])
    print(res)

1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players’ info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2

代码:

# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
N = int(input())
arrive = []
for i in range(N):
    arrive.append([])
    arrive[i] = input().split()
    arrive[i][0] = datetime.strptime(arrive[i][0], '%H:%M:%S')
    arrive[i][1] = timedelta(minutes = int(arrive[i][1]))
line2 = input().split()
tabelNum = int(line2[0])
vipTabelNum = int(line2[1])
vipTableIndex = [int(i) - 1 for i in input().split()]
vipTableIndex = sorted(vipTableIndex)
arrive = sorted(arrive, key = lambda x: x[0])
isvip = []
for i in arrive:
    isvip.append(i[2])
# print(arrive)
tabel = [datetime.strptime('08:00', '%H:%M')] * tabelNum
tabelServeNum = [0] * tabelNum
served = 0
serveTimeDic = {}
res = []
closeTime = datetime.strptime('21:00', '%H:%M')
while served < N and arrive[0][0] < closeTime:
    ealiestEndTime = min(tabel)
    servingTabel = tabel.index(ealiestEndTime)
    if tabel[servingTabel] >= closeTime:
        break
    if arrive[0][0] >= ealiestEndTime:
        if isvip[0] == '1':
            for index in vipTableIndex:
                if tabel[index] <= arrive[0][0]:
                    servingTabel = index
                    break
        else:
            for endtime in tabel:
                if endtime <= arrive[0][0]:
                    servingTabel = tabel.index(endtime)
                    break
        tabel[servingTabel] = arrive[0][0] + min(arrive[0][1], timedelta(hours = 2))
        servingPlayer = datetime.strftime(arrive[0][0], '%H:%M:%S')
        res.append(servingPlayer)
        serveTimeDic[servingPlayer] = [servingPlayer, 0]
        isvip.pop(0)
        arrive.pop(0)
    else:
        if servingTabel in vipTableIndex and '1' in isvip and arrive[isvip.index('1')][0] <= tabel[servingTabel]:
            vipServedIndex = isvip.index('1')
            servingVip = datetime.strftime(arrive[vipServedIndex][0], '%H:%M:%S')
            res.append(servingVip)
            serveTimeDic[servingVip] = \
            [datetime.strftime(tabel[servingTabel], '%H:%M:%S'), \
            round((tabel[servingTabel] - arrive[vipServedIndex][0]).total_seconds() / 60. + 0.01)]
            tabel[servingTabel] = tabel[servingTabel] + min(arrive[vipServedIndex][1], timedelta(hours = 2))
            isvip.pop(vipServedIndex)
            arrive.pop(vipServedIndex)
        else:
            if isvip[0] == '1':
                for index in vipTableIndex:
                    if tabel[index] == ealiestEndTime:
                        servingTabel = index
                        break
            serving = datetime.strftime(arrive[0][0], '%H:%M:%S')
            res.append(serving)
            serveTimeDic[serving] = \
            [datetime.strftime(tabel[servingTabel], '%H:%M:%S'), \
            round((tabel[servingTabel] - arrive[0][0]).total_seconds() / 60. + 0.01)]
            tabel[servingTabel] = tabel[servingTabel] + min(arrive[0][1], timedelta(hours = 2))
            isvip.pop(0)
            arrive.pop(0)
    tabelServeNum[servingTabel] += 1
    served += 1
# print(serveTimeDic)
for key in res:
    tempres = key + ' ' + serveTimeDic[key][0] + ' ' + str(serveTimeDic[key][1])
    print(tempres)
print(' '.join([str(i) for i in tabelServeNum]))

喵了个咪的,提交了17次才全对,一世英名都差点毁在这道题上
开始的ifelse写的条件不互补,大概想了一下觉得对就再也没找出毛病来,倒是第一个大测试点能对,但有四个点怎么都过不去,具体去看提交列表吧。。。
vipTableIndex是一行全部读进来的,一开始是一行一行读的竟然没出错==
这个题和以前的排队类型题目相比,最大的两个坑在于:
1. 时间超过两个小时的要叫停,也就是timedelta不能大于hours = 2;
2. 以前呐,都是每次找结束时间最短(最快)的窗口,有多个一样的最短时间时,自动会返回索引值最小的,刚好和题意相符,但是这个题,有vip球员,当结束时间相同的时候,他是去vip桌,没有vip桌,他才去标号最小的空桌
3. 还有个问题是,当来了不用排队的时候,是找标号最小的空桌,比如3号桌是08:00空的,2号桌是08:30空的,球员是09:00来的,这时候他去的是2号桌,而不是结束时间最前的3号桌。
所以整个程序调整思路以后,把ifelse的条件重新理一下:
先看用不用排队,也就是来的时候有没有空桌:
如果不用排队:
这时候看来的人是不是vip,如果是vip,则找标号最小的vip空桌,否则找标号最小的空桌
如果排队:
如果当前空桌是vip桌且桌子空出来的时候队伍里面有vip球员,那么这个vip球员先上桌;
否则:
如果队伍里面第一个人是vip,那么要重新看一下有没有同时空出来的vip桌(注意点2),如果有,应把当前桌改为标号最小的vip桌;
然后常规更新桌子结束时间等,这时候已经排队了,桌子结束时间是自加打球时间,不要搞错了!!检查一下!!
还有就是round四舍五入的问题round(0.5)=0, round(0.51)就是1了,然后0.01分钟刚好不足1秒,所以在timedelta算出来的分钟再+0.01分钟

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值