读题+分析:10min
写代码:70min
虽然是一遍过,但不是很满意,用时太长了
不过考虑到是隔了好久再做CSP,手生是正常的
毕竟和平时写爬虫写Django不一样,CSP对细节的要求更高,稍有不慎失去的可能就是几十分
所以大体来说差强人意吧
class AppleTree:
tree_name = 0
apples_num = 0
cut_sum = 0
def __init__(self, original_apples_num, tree_name):
# 初始化一个苹果树对象时传入原有苹果个数和编号(注意:从1开始)
self.apples_num = original_apples_num
self.tree_name = tree_name
def cut_apples(self, cut_num):
self.apples_num += cut_num
# 苹果数减少,因为传入cutnum为非正数
self.cut_sum -= cut_num
# 总的被修剪的个数增加
if __name__ == "__main__":
tree_num, cut_round = input().split(" ")
tree_num = int(tree_num)
cut_round = int(cut_round)
tree_objects = []
# 苹果树对象列表
for tn in range(tree_num):
tree_objects.append(AppleTree(0, tn + 1))
# 以苹果数为0,传入名字为tn + 1初始化一个苹果树对象并添加至列表
original_apples_num, cut_num_list = input().split(" ", 1)
# 先只分一次把原始苹果数和多次修剪的个数分开
original_apples_num = int(original_apples_num)
# 得到每棵树的原始苹果数
cut_num_list = list(map(int, cut_num_list.split(" ")))
# 然后再把修剪个数分开并分别转为int型
tree_objects[tn].apples_num = original_apples_num
# 修改苹果树对象的初始苹果数
for cut_num in cut_num_list:
# 修剪一棵树cut_round轮,即cut_num_list的长度
tree_objects[tn].cut_apples(cut_num)
all_apples_count = 0
for to in tree_objects:
all_apples_count += to.apples_num
# 计算所有苹果树的苹果总数
tree_objects.sort(key=lambda i: i.cut_sum, reverse=True)
# lambda的优雅用法:对对象数组、字典等根据某一特定项大小进行逆序稳定排序
print(all_apples_count, tree_objects[0].tree_name, tree_objects[0].cut_sum)