题解 | #汽水瓶#递归函数/循环条件嵌套语句
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
''' 1/3=0---1 0 2/3=0---2 0 (0+2+1)/3=1---0 0+1=1 3/3=1---0 1 4/3=1---1 1 (1+1+1)/3=1---0 1+1=2 5/3=1---2 1 (1+2)/3=1---0 1+1=2 6/3=2---0 2 (2+0+1)/3=1---0 2+1=3 10/3=3---1 3 (3+1)/3=1---1 1 (1+1+1)/3=1---0 3+1+1=5 11/3=3---2 3 (3+2)/3=1---2 1 (1+2)/3=1---0 3+1+1=5 12/3=4---0 4 (4+0)/3=1---1 1 (1+1+1)/3=1---0 4+1+1=6 ''' '''1、循环语句:计算过程较为复杂 while True: drink=0 n=int(input()) if n>0: if n==1: drink += 0 elif n>1: drink += 1 while n//3+n%3>1: drink += n//3 if n//3+n%3>=3: borrow=0 else: borrow=3-n//3-n%3 n = n//3+n%3+borrow elif n==0: print() break print(drink) ''' #2、简化思路:观察n与瓶数的关系,发现n//2=瓶数 ''' while True: n=int(input()) if n>0: print(n//2) if n==0: #print() break while True: n=int(input()) if n==0: # break条件放在前面,更简洁。 break print(n//2) ''' #3、递归思路: ''' import sys def f(n): if n == 0: return 0 if n == 1: return 0 if n >=2: return f(n-2) + 1 if __name__ == '__main__': data = sys.stdin for x in data: x = int(x.strip()) if x != 0: print(f(x)) ''' def max_bottles(n): result = n // 3 bottles = n // 3 + n % 3 if bottles == 2: # 空瓶数量为2,可以借一瓶,此后不能再兑换 result += 1 elif bottles < 2: # 空瓶数小于2,无法兑换 result += 0 else: result += max_bottles(bottles) return result while True: try: n = int(input()) if n == 0: break else: print(max_bottles(n)) except: break