题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
import sys import math # 方法一:通过规律总结得出公式 # for line in sys.stdin: # a = line.split() # n = int(a[0]) # if n: # print(n//2) # else: # pass # 方法二: def fn(n, m=3): # 可自定义大于 2 个空水瓶对换 global k k += 1 # 统计递归调用次数 if n < m - 1: li.append(0) if n == m - 1: # 满足可以借的条件 li.append(1) # 能借就能多一瓶,不能借就不添加 if n >= m: # new_n = n // m + n % m new_n = sum(divmod(n, m)) li.append(n // m) fn(new_n, m) return sum(li) for line in sys.stdin: a = line.split() n = int(a[0]) li = [] k = 0 m = 3 if n: result = fn(n, m) print(result) # print(n, k, result, int(n / (m - 1)), math.floor(n / (m - 1)), n // (m - 1))