阿里巴巴找黄金宝箱(I)
python
import sys
# 输入
input = sys.stdin.readline().strip()
nums = list(map(int, input.split(',')))
n = len(nums)
# 初始化左右两部分的和
left_sum = 0
right_sum = sum(nums)
# 遍历每个箱子,判断是否为黄金宝箱
flag = False # 是否找到黄金宝箱的标志
for i in range(n):
if i != 0: # 左部分的和不包括第一个数字
left_sum += nums[i - 1]
right_sum -= nums[i] # 右部分的和不包括当前数字
if left_sum == right_sum: # 判断是否为黄金宝箱
print(i) # 输出黄金宝箱的编号
flag = True # 找到了黄金宝箱
break # 直接退出循环
# 输出结果
if not flag: # 如果没有找到黄金宝箱
print(-1) # 输出-1
中文分词模拟器
python
def word_segmentation(s, dictionary):
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
result = []
for i in range(1, n + 1):
for j in range(i - 1, -1, -1):
if dp[j] and s[j:i] in dictionary:
dp[i] = True
break
if not dp[n]:
return [s[i] for i in range(n)]
i = n
while i > 0:
for j in range(i - 1, -1, -1):
if dp[j] and s[j:i] in dictionary:
result.insert(0, s[j:i])
i = j
break
return result
if __name__ == "__main__":
input_string = input().strip()
dictionary = input().strip().split(',')
result = word_segmentation(input_string, dictionary)
print(','.join(result))
单行道汽车通行时间
python
def solution_01():
M_N = input().split(" ")
M = int(M_N[0])
N = int(M_N[1])
s_list = []
for i in range(M):
s_list.append(int(input()))
t_list = [N * 1.0 / s_list[0] + 0.0]
for i in range(1, M):
tmp = N * 1.0 / s_list[i] + i
t = max(tmp, t_list[i - 1])
t_list.append(t)
print(t_list[M - 1] - M + 1)
if __name__ == '__main__':
solution_01()
手机App防沉迷系统
python
class App:
def __init__(self, name, p, start, end):
self.name = name # 应用名称
self.p = p # 优先级
self.start = start # 开始时间
self.end = end # 结束时间
def trans_to(data):
arr = data.split(":")
hour = int(arr[0]) # 小时部分
minute = int(arr[1]) # 分钟部分
return hour * 60 + minute # 将时间转换为分钟表示
def trans_to_app(s):
arr = s.split(" ")
name = arr[0] # 应用名称
p = int(arr[1]) # 优先级
start = trans_to(arr[2]) # 开始时间
end = trans_to(arr[3]) # 结束时间
return App(name, p, start, end)
n = int(input()) # 输入应用的数量
apps = []
# 读取每个应用的信息
for i in range(n):
s = input()
temp = trans_to_app(s)
# 如果开始时间晚于结束时间,忽略该应用
if temp.start > temp.end:
continue
apps.append(temp)
res = []
# 遍历每个应用
for i in range(len(apps)):
ids = []
# 找到与当前应用时间段有重叠的应用
for j in range(len(res)):
if res[j].start <= apps[i].end and apps[i].start <= res[j].end:
ids.append(j)
mx = -1
# 找到重叠应用中优先级最高的
for j in ids:
mx = max(mx, res[j].p)
# 如果当前应用优先级更高,则移除重叠应用,添加当前应用
if mx < apps[i].p:
for j in reversed(ids):
res.pop(j)
res.append(apps[i])
time = trans_to(input()) # 输入时间
ans = "NA"
# 查找当前时间所在的应用
for app in res:
if app.start <= time and time <= app.end:
ans = app.name
break
print(ans) # 输出结果
5G 网络建设
python
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
cities = len(isConnected)
visited = set()
provinces = 0
for i in range(cities):
if i not in visited:
Q = collections.deque([i])
while Q:
j = Q.popleft()
visited.add(j)
for k in range(cities):
if isConnected[j][k] == 1 and k not in visited:
Q.append(k)
provinces += 1
return provinces
靠谱的车
python
def calculate_actual_cost(surface_reading):
actual_cost = 0
surface_reading_str = str(surface_reading)
for i in range(len(surface_reading_str)):
digit = int(surface_reading_str[i])
if digit == 4:
# 跳过数字 4
continue
elif digit > 4:
# 对大于 4 的数字减一
digit -= 1
# 计算当前位数对应的实际费用,并累加到总费用中
actual_cost += digit * (9 ** (len(surface_reading_str) - i - 1))
return actual_cost
# 输入获取
surface_reading = int(input())
# 计算实际产生的费用并输出结果
print(calculate_actual_cost(surface_reading))