题目
请找到一个大于 2022的最小数,这个数转换成十六进制之后,所有的数位(不含前导 0)都为字母(A 到F)。
请将这个数的十进制形式作为答案提交。
思路
暴力直接遍历。转十六进制函数:hex(),判断全为字母:words.isalpha()
也可以直接算,2022的十六进制:0x7e7,则全是字母的最小十六进制为:0xaaa,将该数转化为十进制输出。
代码
num = 2022
while True:
hexnum = hex(num)[2:]
if all(x.isalpha() for x in hexnum): #all()返回一个bool变量
print(num)
break
num += 1
# a = 2022
# b = hex(a)
# # b=0x7e7
# print(int(0xaaa))
tips:
二进制 bin()
八进制 oct()
十进制int()
十六进制hex()
int(‘1010’,2) --> 10
int(‘12’,8) --> 10
int(‘a’,16) --> 10
题目
"饱了么"外卖系统中维护着N 家外卖店,编号 1 ∼ N。每家外卖店都有 一个优先级,初始时 (0 时刻) 优先级都为 0。
每经过 1 个时间单位,如果外卖店没有订单,则优先级会减少 1,最低减 到 0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 2。
如果某家外卖店某时刻优先级大于 5,则会被系统加入优先缓存中;如果 优先级小于等于 3,则会被清除出优先缓存。
给定T 时刻以内的M 条订单信息,请你计算T 时刻时有多少外卖店在优先缓存中?
思路
只通过3个用例,但是不知道哪里出了问题 也没办法看见每个用例的具体情况。
代码
import os
import sys
# 请在此输入您的代码
N, M, T = map(int, input().split())
stor_dict = {}
for i in range(M):
ts, stor_id = map(int, input().split())
if stor_id not in stor_dict.keys():
stor_dict[stor_id] = [ts]
else:
stor_dict[stor_id].append(ts)
count = 0
for i in stor_dict.keys(): #对每家店的订单进行遍历
temp = sorted(stor_dict[i]) # 对订单的时间进行排序
order = 2 #初始化优先级为2 因为从第一个有订单的时间开始 order = 2 因为优先级最低为0
for j in range(1, len(temp)): #对第二个订单时间开始遍历
if temp[j] <= T: #存在T时刻是订单的中间时刻 当订单时间大于该时刻就跳出循环 对当前的优先级进行判断
if temp[j] - temp[j-1] <= 1: #后面一个订单时间与前面订单时间相同或者相隔为1 则优先级+2
order += 2
else: #相隔>1 oder = 当前oder - (相隔时间) + 1 + 2
order =order - (temp[j] - temp[j-1]) + 3
if order < 0: # order最小为0
order = 0
else:
break
if temp[-1] < T: #当最后一个订单时间<T,则这段空闲时间order需要减去两者之间的差值
order = order - (T - temp[-1])
if order > 5:
count += 1
print(count)
32万+

被折叠的 条评论
为什么被折叠?



