重生之我是带学生(2021.09.24)

PTA练习题

6.输出第一个来和最后一个走的人的ID (已做完)

        开始用的C++写了好半天,始终处理不好带空格的输入字符串,索性摆大烂用python做。

        整体思路,每个Person类对象中有data列表用来存放每行的输入数据,空格分开后,每个时间字符串的位置是固定的,最后hour1,hour2,minute1,minute2,second1,second2六个列表分别存放所有人出入的时、分、秒,再通过几个变量和列表用来处理存在相同小时、分钟、秒(还不确定检查点是否有完全相同的时间)时的情况。

        处理相同时段的具体算法思路为(最早与最晚同理):遍历比较每个对象的进入小时与hour1中最小的小时(格式化的数据比较不必把字符串转为整形),有相同则先去掉hour1中非相同的项再比分钟,无相同则直接输出对应id,比分钟时同理,最后得到唯一(?)的最小时间输出其对应对象的id。

import sys

message = []
n = int(input())


class Person:

    def __init__(self):
        self.id = ''
        self.data = []


for i in range(n):
    message.append(input())

example = {}
for i in range(n):
    example['obj' + str(i)] = Person()

for i in range(n):
    tmp = message[i].split()
    example['obj' + str(i)].data.append(tmp[0])
    example['obj' + str(i)].data.append(tmp[1][0] + tmp[1][1])
    example['obj' + str(i)].data.append(tmp[2][0] + tmp[2][1])
    example['obj' + str(i)].data.append(tmp[1][3] + tmp[1][4])
    example['obj' + str(i)].data.append(tmp[2][3] + tmp[2][4])
    example['obj' + str(i)].data.append(tmp[1][6] + tmp[1][7])
    example['obj' + str(i)].data.append(tmp[2][6] + tmp[2][7])

# for i in range(n):
#     print(example['obj' + str(i)].data)

hour1 = []
hour2 = []
minute1 = []
minute2 = []
second1 = []
second2 = []

for i in range(n):
    hour1.append(example['obj' + str(i)].data[1])
    hour2.append(example['obj' + str(i)].data[2])
    minute1.append(example['obj' + str(i)].data[3])
    minute2.append(example['obj' + str(i)].data[4])
    second1.append(example['obj' + str(i)].data[5])
    second2.append(example['obj' + str(i)].data[6])

count_h = 0
count_h_same = 0
count_m = 0
count_m_same = 0
count_s = 0
count_s_same = 0

same_h = []
same_m = []
same_s = []

for i in range(n):
    for j in range(n):
        if min(hour1) == example['obj' + str(j)].data[1]:
            same_h.append(example['obj' + str(j)])
            count_h = count_h + 1
        # else:
            # print(hour1)
            # print(min(hour1))
            # print(example['obj' + str(j)].data[1])
            # hour1.remove(example['obj' + str(j)].data[1])
            # minute1.remove(example['obj' + str(j)].data[3])
            # second1.remove(example['obj' + str(j)].data[5])
    # print(min(hour1))
    # print(example['obj' + str(i)].data[1])

    if count_h != n:
        count_h_same = 1
    print(count_h_same)

    if (min(hour1) == example['obj' + str(i)].data[1]) and (count_h_same == 0):
        print(example['obj' + str(i)].data[0] + ' ')
    else:
        for k in range(same_h.__len__()):
            if min(minute1) == same_h[k].data[3]:
                same_m.append(same_h[k])
                count_m = count_m + 1
            # else:
                # hour1.remove(same_h[k].data[1])
                # minute1.remove(same_h[k].data[3])
                # second1.remove(same_h[k].data[5])
        if (min(minute1) == example['obj' + str(j)].data[3]) and (count_m == 1):
            print(example['obj' + str(i)].data[0] + ' ')
        else:
            for l in range(same_m.__len__()):
                if min(second1) == same_m[l].data[5]:
                    same_s.append(same_m[l])
                    count_s = count_s + 1
            if (min(second1) == example['obj' + str(k)].data[5]) and (count_s == 1):
                print(example['obj' + str(i)].data[0] + ' ')

重新简化后(9.25)

其实没必要纠结时分秒,直接整个时间字符串比就ok,前面白搞那么多,麻了

import sys

message = []  # 存放一整行字符串
n = int(input())


# 定义Person类
class Person:

    def __init__(self):
        self.id = ''
        self.time1 = ''
        self.time2 = ''
        self.data = []


for i in range(n):
    message.append(input())

# 通过字典批量创建Person类对象
example = {}
for i in range(n):
    example['obj' + str(i)] = Person()

for i in range(n):
    tmp = message[i].split()
    example['obj' + str(i)].id = tmp[0]
    example['obj' + str(i)].time1 = tmp[1]
    example['obj' + str(i)].time2 = tmp[2]

mintime = '24:00:00'
maxtime = '00:00:00'
minid = ''
maxid = ''

# 找出最早时间和最晚时间
for i in range(n):
    if example['obj' + str(i)].time1 <= mintime:
        mintime = example['obj' + str(i)].time1

    if example['obj' + str(i)].time2 >= maxtime:
        maxtime = example['obj' + str(i)].time2

# 找出对应ID
for i in range(n):
    if example['obj' + str(i)].time1 == mintime:
        minid = example['obj' + str(i)].id
    if example['obj' + str(i)].time2 == maxtime:
        maxid = example['obj' + str(i)].id

print(minid, maxid)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值