一道有趣的计算题
不管用什么语言算出来就好, 大家有没有空 算一下{:5_92:} Python
def jipingjiu(money):
beer = money / 2# 不考虑找零money%2
btt, cap = money/2, money/2 #买过酒得到的酒瓶及瓶盖
flag = 1
brr = 0#如果可以赊酒的话
while flag:
a = btt/2 #酒瓶换酒
b = cap/4 #瓶盖换酒
tmp = a + b - brr
if tmp > 0 or brr == 2:
beer += a + b
btt = a + b - brr + btt%2
cap = a + b - brr + cap%4
brr = 0
flag = btt + cap
# flag = btt/2 + cap/4 #如果不能赊酒的话
else:
btt += 1 #赊一瓶酒
cap += 1
brr += 1
print "喝了{}瓶酒,赊了{}瓶酒,剩余{}酒瓶及{}瓶盖".format(beer, brr, btt,cap)
jipingjiu(10) 新手·ing 发表于 2018-2-11 14:33
Python
btt, cap = money/2, money/2 #买过酒得到的酒瓶及瓶盖 这一句不懂, 大佬 Ъγ:_小ツ雨oο 发表于 2018-2-11 14:41
btt, cap = money/2, money/2 #买过酒得到的酒瓶及瓶盖 这一句不懂, 大佬
??除以二 Ъγ:_小ツ雨oο 发表于 2018-2-11 14:41
btt, cap = money/2, money/2 #买过酒得到的酒瓶及瓶盖 这一句不懂, 大佬
找钱吗 本帖最后由 °蓝鲤歌蓝 于 2018-3-8 16:55 编辑
def PiJiu(battle,cup,boo):
if cup < 2 and boo < 4:
return battle
else:
battle1 = cup // 2 + boo // 4
cup = battle1 + cup % 2
boo = battle1 + boo % 4
return battle + PiJiu(battle1, cup, boo)
# 10元5瓶酒,5个盖子,5个空瓶
Pi = PiJiu(5,5,5)
print(Pi)
答案是15瓶。
前段时间卖汽水,现在过来买啤酒。放心,我什么都不会买的。
°蓝鲤歌蓝 发表于 2018-2-11 15:27
答案是15瓶。
前段时间卖汽水,现在过来买啤酒。放心,我什么都不会买的。
{:5_92:}, 为啥都用py里 Ъγ:_小ツ雨oο 发表于 2018-2-11 15:36
, 为啥都用py里
我学Python比较多,C和Java就会一点点,
思路是有,但是用另外两种语言写不出来。 c语言应该 好些一点吧 import tkinter as tk
def hePijiu(qian):
pijiushu = qian // 2
pinyu = 0
gaiyu = 0
zpijiushu = pijiushu
while pijiushu>=1 :
pijiushu_qian = pijiushu
pingyu_qian = pinyu
gaiyu_qian = gaiyu
pijiushu= (pijiushu_qian+pingyu_qian)//2 + (pijiushu_qian+gaiyu_qian)//4
pinyu = (pijiushu_qian+pingyu_qian) % 2
gaiyu = (pijiushu_qian+gaiyu_qian) % 4
zpijiushu=zpijiushu + pijiushu
return zpijiushu
master = tk.Tk()
tk.Label(master, text="钱数:").grid(row=0)
e1 = tk.Entry(master)
e1.delete(0,tk.END)
e1.insert(0,'10')
e1.grid(row=0, column=1, padx=10, pady=5)
v = tk.StringVar()
label = tk.Label(master, textvariable=v)
label.grid(row=3, column=1, padx=10, pady=5)
def show():
qian = e1.get()
try:
q = int(qian)
v.set("总共能喝%s瓶" % hePijiu(q))
e1.delete(0, tk.END)
except:
v.set("请输入一个数字!")
e1.delete(0, tk.END)
tk.Button(master, text="计算", width=10, command=show)\
.grid(row=3, column=0, sticky=tk.W, padx=10, pady=5)
tk.mainloop() #酒瓶喝酒问题
L =
while L != 0:
L += L
L = L % 2 + L
L = L % 4 + L
L = L // 2 + L // 4
print('一共喝了%d瓶,还剩%d个瓶盖和%d个空瓶'%(L,L,L))
一共喝了15瓶,还剩1个瓶盖和3个空瓶
页:
[1]