- 论坛徽章:
- 0
|
本帖最后由 tubocurarine 于 2011-07-22 10:01 编辑
80.- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- def count_water(bottle_num, cover_num):
- print("Caculate: bottle_num:%d, cover_num:%d"%(bottle_num, cover_num))
- totle = 0
- tmp = 0
- if bottle_num == 1:
- print "We need to borrow 1 bottle from others."
- totle += 1
- cover_num += 1
- bottle_num = 0
- elif bottle_num >= 2:
- tmp = bottle_num / 2
- totle += tmp
- bottle_num = tmp + bottle_num%2
- cover_num += totle
- if cover_num == 2:
- print "We need to borrow 2 covers from others"
- totle += 1
- cover_num = -1
- bottle_num += 1
- elif cover_num == 3:
- print "We need to borrow 1 cover from others."
- totle += 1
- cover_num = 0
- bottle_num += 1
- elif cover_num >=4:
- tmp = cover_num / 4
- totle += tmp
- bottle_num += tmp
- cover_num = tmp + cover_num % 4
- if bottle_num == 0 and cover_num < 2:
- if cover_num >= 0:
- print("Finished caculating: bottle_num:%d, cover_num:%d"%\
- (bottle_num, cover_num))
- return totle
- else:
- print("Step back!")
- return totle - 1
- else:
- return totle + count_water(bottle_num, cover_num)
- if __name__ == '__main__':
- init = int(raw_input("Please input initial value:"))
- if init <= 0:
- print "Please make sure init value is bigger than zero."
- exit(1)
- print("Input: %d, output: %d"%(init, init+count_water(init, init)))
复制代码 |
|