力扣每日一题(三十二)
作者:互联网
仅以此纪录每日LootCode所刷题目。
题目描述:
示例:
思路:
这道题可以将其分为两部分,第一部分是算出week的数量,第二部分是算出day的数量。week的数量可以使用int(n/7来求),day的数量可以使用n-week*7来求,之后分类讨论week为0和week大于0的情况。思路比较简单,代码也比较易懂,唯一不足的就是变量使用的太多了。
代码:
class Solution:
def totalMoney(self, n: int) -> int:
week = int(n / 7)
day = n - 7*week
if week > 0:
weekcount = 0
weekone = 28
for i in range(week):
weekcount += weekone
weekone += 7
dayone = 1 + week
daycount = 0
for i in range(day):
daycount += dayone
dayone += 1
else:
weekcount = 0
daycount = 0
for i in range(1,day+1):
daycount += i
return daycount + weekcount
标签:week,三十二,dayone,int,每日,力扣,daycount,weekcount,day 来源: https://blog.csdn.net/touxing777/article/details/122514749