贪心:未到达终点前每一次走的越远越好;由于步数从1~5,不担心直接越过终点, 所以直接对五求商向上取整就好。不过这种题目假设给个能否走到的变体条件可能就要靠动态规划才比较好解了。
from math import ceil x = int(input()) print(ceil(x/5))
动态规划:TLE
x = int(input()) steps = [1, 2, 3, 4, 5] dp = [float('inf')] * (x+1) # dp[i]还剩多少步需要走几次 dp[0] = 0 for i in range(x+1): for s in steps: dp[i] = min(dp[i-s]+1, dp[i]) if i >= s else dp[i] print(dp[-1])
镜像题目:https://blog.nowcoder.net/n/5b407a78951f460d85f1c27378338860