免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3604 | 回复: 8
打印 上一主题 下一主题

一个小的趣味题,写来写去写不对,求救 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-21 22:55 |只看该作者 |倒序浏览
本帖最后由 hzw0705 于 2011-07-22 00:18 编辑

水1元一瓶,两个瓶子可以换一瓶 4个盖子也可以换一瓶,问20块钱你最多能喝到多少瓶水。
写个python小程序应该可以实现啊,写来写去写不对

论坛徽章:
0
2 [报告]
发表于 2011-07-21 23:42 |只看该作者
75?

论坛徽章:
0
3 [报告]
发表于 2011-07-22 08:48 |只看该作者

  1. def count_water(water_num,bottle_num,cover_num):
  2.     if bottle_num < 2 and cover_num <4:
  3.         return water_num
  4.     else:
  5.         new_water_num=bottle_num//2+cover_num//4
  6.         cover_num=cover_num%4+new_water_num
  7.         bottle_num=bottle_num%2+new_water_num
  8.         return count_water(water_num+new_water_num,bottle_num,cover_num)
  9. if __name__ == "__main__":
  10.     print count_water(20,20,20)

复制代码

论坛徽章:
0
4 [报告]
发表于 2011-07-22 09:51 |只看该作者
本帖最后由 tubocurarine 于 2011-07-22 10:01 编辑

80.
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def count_water(bottle_num, cover_num):

  4.     print("Caculate: bottle_num:%d, cover_num:%d"%(bottle_num, cover_num))
  5.     totle = 0
  6.     tmp = 0

  7.     if bottle_num == 1:
  8.         print "We need to borrow 1 bottle from others."
  9.         totle     += 1
  10.         cover_num += 1
  11.         bottle_num = 0
  12.     elif bottle_num >= 2:
  13.         tmp = bottle_num / 2
  14.         totle += tmp
  15.         bottle_num = tmp + bottle_num%2
  16.         cover_num += totle

  17.     if cover_num == 2:
  18.         print "We need to borrow 2 covers from others"
  19.         totle += 1
  20.         cover_num = -1
  21.         bottle_num += 1
  22.     elif cover_num == 3:
  23.         print "We need to borrow 1 cover from others."
  24.         totle += 1
  25.         cover_num = 0
  26.         bottle_num += 1
  27.     elif cover_num >=4:
  28.         tmp = cover_num / 4
  29.         totle += tmp
  30.         bottle_num += tmp
  31.         cover_num = tmp + cover_num % 4

  32.     if bottle_num == 0 and cover_num < 2:
  33.         if cover_num >= 0:
  34.             print("Finished caculating: bottle_num:%d, cover_num:%d"%\
  35.                       (bottle_num, cover_num))
  36.             return totle
  37.         else:
  38.             print("Step back!")
  39.             return totle - 1
  40.     else:
  41.         return totle + count_water(bottle_num, cover_num)

  42. if __name__ == '__main__':
  43.     init = int(raw_input("Please input initial value:"))
  44.     if init <= 0:
  45.         print "Please make sure init value is bigger than zero."
  46.         exit(1)

  47.     print("Input: %d, output: %d"%(init, init+count_water(init, init)))

复制代码

论坛徽章:
0
5 [报告]
发表于 2011-07-24 01:06 |只看该作者
>>> def by_water(money):
        #初始化,先确定money能买回几瓶水
        water_have = money
        #初始化,买了多少平水就有多少瓶子和盖子
        bottle_have = water_have
        cover_have = water_have
        #只要拥有的瓶子或盖子数满足换水的条件,那么能买到水的数量就必然增加
        while bottle_have >= 2 or cover_have >= 4:
                #先把手头上的瓶子换了,看下能换多少平水
                water_new = bottle_have / 2
                #换水后拥有的瓶子数:剩下的+新换回的
                bottle_have = bottle_have % 2 + water_new
                #能买的水数量增加了water_new瓶
                water_have += water_new
                #盖子的数量增加了water_new个
                cover_have += water_new
                #下面是用盖子换水的过程,与上面类似
                water_new = cover_have / 4
                cover_have = cover_have % 4 + water_new
                bottle_have += water_new
                water_have += water_new
        return water_have

>>> by_water(20)
75
>>> by_water(10)
35
>>>

论坛徽章:
0
6 [报告]
发表于 2011-07-24 01:16 |只看该作者
写什么程序

瓶子 0.5, 盖子0.25,所以一瓶水0.25

20/ 0.25 = 80

OK 了

论坛徽章:
0
7 [报告]
发表于 2011-07-25 11:39 |只看该作者
回复 6# shhgs


    这个算法不对的吧。。。。。。

论坛徽章:
0
8 [报告]
发表于 2011-07-25 17:30 |只看该作者
发个自己写的..
  1. # -*-coding:utf-8-*-
  2. #水1元一瓶,两个瓶子可以换一瓶 4个盖子也可以换一瓶,问20块钱你最多能喝到多少瓶水
  3. def count_bottle(money):
  4.     bottle = money
  5.     cover = money
  6.     count = money
  7.     while bottle/2 or cover/4 :
  8.         count = count + bottle/2 + cover/4
  9.         temp = bottle
  10.         bottle = bottle/2 + bottle%2 + cover/4
  11.         cover = cover/4 + cover%4 + temp/2
  12.     print count

  13. if __name__ == '__main__':
  14.     while True:
  15.         try:
  16.             money = int(raw_input('输入钱数-->'))
  17.             break
  18.         except ValueError:
  19.             print "请输入整数!"
  20.     count_bottle(money)
复制代码

论坛徽章:
0
9 [报告]
发表于 2011-07-25 19:36 |只看该作者
def heshui(num_water,num_bottle,num_hat):
    if num_bottle==1:
        num_water+=1
        num_bottle=0
        num_hat+=1
    if num_hat<4:
        num_water+=1
        num_hat-=1
    if num_hat==num_bottle==0:
        return num_water
    else:
        huanshui=num_bottle/2+num_hat/4
        num_water+=huanshui
        num_bottle=huanshui+num_bottle%2
        num_hat=huanshui+num_hat%4
        print num_water,num_bottle,num_hat
        return heshui(num_water,num_bottle,num_hat)

def buy_water(yuan):
    return heshui(yuan,yuan,yuan)
print buy_water(20)










output:
35 15 15
45 11 13
53 9 9
59 7 7
63 5 7
66 4 6
69 3 5
71 3 3
73 2 3
75 1 3
77 1 1
79 0 1
80
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP