方法1: python
import sys pyin = [int(i) for i in sys.stdin.read().split()] for n in pyin: z_num = 0 if n == 0: exit(); if n > 100 and n < 0: exit(); kong = n while kong // 3: yu = kong % 3 zheng = kong // 3 z_num += zheng kong = yu + zheng if kong % 3 == 2: z_num += 1 print(z_num)
这种做法是模拟+递归。就按题目假设的例子来进行分析,一开始是10个空瓶子,换了10/3=3瓶,之后这三瓶又加上10%3=1瓶,一共4瓶。这4瓶我们又去换,就是又一次整除3的过程,换了4/3=1瓶,再加上4%3=1瓶。此时还有2瓶,先不考虑去找老板借,我们不难发现,之前我们在做的是一个重复过程。
方法2:python
import sys inpy = [int(i) for i in sys.stdin.read().split()] for i in inpy: if i == 0: break print(i // 2)
通过逻辑推理,只要把初始空瓶除以二即可。
方法3: c
int getValid(int number) { int x=0; int y=0; int rtn=0; if(number == 1) { return 0; } else if(number == 2) { return 1; } else { x=number%3; y=number/3; rtn = y; rtn += getValid(x+y); return rtn; } } void main (void) { int num,result=0; while(scanf("%d",&num)&&(num!=0)) { result = getValid(num); printf("%d\n",result); } }