啤酒2塊1瓶,4個蓋換1瓶,2個空瓶換1瓶,10塊錢可以喝幾瓶 ?為什麼?
按照不能借的規則,答案是15瓶。
如可以借,借錢還錢,借酒還酒,則答案是20瓶。
可以借的話可以有兩種解法,第一,從10塊錢買五瓶開始推,直到剩下的瓶和蓋不夠換開始借。第一,從兩塊錢買一瓶借三瓶,得到兩塊錢可以喝四瓶,從而十塊錢可以喝二十瓶。
但是20瓶不能用一個空瓶值一元,一個瓶蓋值五毛,一瓶酒(液體部分)五毛,的思路,也就是說不能用方程的思維。因為這樣只計算了價值,而忽略了這事情只能通過組合交換實現,以及完整的酒一開始必然包括三部分這一事實。用10除以0.5算出來的答案,是碰巧的。比如,在借錢還錢,借酒還酒的規則下,不能用一塊錢喝到酒,不能用一塊錢喝酒,那自然不能用三塊,七塊,五塊五非偶數都不行。
這道題的有幾個重要的思想:
1.統籌規劃求最優解
2.區分情況討論,借或者不能
3.化歸,10塊錢的問題用兩塊錢來推演。
4.賒銷賒購
這是一道好題,出題人沒有問9塊錢能喝多少,也沒有問100塊錢能喝多少。它讓你能陷入錯誤的思維得到正確的答案,也讓你能在正確的思維里可以求解。
應該完善了,留下答案,以後孩子用#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
price = 2
bottle_value = 2
caps_value = 4
def cheers(money,bottle_num,caps_num,count):
#print(">&>",money,bottle_num,caps_num,count)
curr_num = money // price + bottle_num // bottle_value + caps_num // caps_value
money = money % price
count += curr_num
bottle_num = curr_num + bottle_num % bottle_value
caps_num = curr_num + caps_num % caps_value
print(money,bottle_num,caps_num,count)
if (money&>=price or bottle_num&>=bottle_value or caps_num&>=caps_value):
cheers(money,bottle_num,caps_num,count)
else:
print(">&>&> ",count," &<&<&<")
cheers(10,0,0,0)
0 5 5 5
0 4 4 8
0 3 3 11
0 2 4 12
0 2 2 14
0 1 3 15
&>&>&> 15 &<&<&<
[Finished in 0.1s]
1.換得5瓶,得到5個瓶蓋,5個空瓶
2.換得3瓶,還剩4個瓶蓋,4個空瓶
3.換得3瓶,還剩3個瓶蓋,3個空瓶
4.換得1瓶,還剩4個瓶蓋,2個空瓶
5.換得2瓶,還剩2個瓶蓋,2個空瓶
6.換得1瓶,還剩3個瓶蓋,1個空瓶
無法繼續換 所以應該是15瓶
遞推迭代演算法浪費資源,直接用m=4k-5運算即可;
喝的總瓶數m=4*初次瓶數k-5,詳細過程參見我的解答(點擊我的用戶名,最下方的那條解答)。
4塊錢能喝3瓶;
6塊錢能喝7瓶;
8塊錢能喝11瓶;
10塊錢能喝15瓶;
12塊錢能喝19瓶;
14塊錢能喝23瓶;
16塊錢能喝27瓶;
18塊錢能喝31瓶;
20塊錢能喝35瓶;
30塊錢能喝55瓶;
40塊錢能喝75瓶;
50塊錢能喝95瓶;
60塊錢能喝115瓶;
70塊錢能喝135瓶;
#include &
main()
{
int money=10;
int i,j,k;
k=0;
i=0;
j=0;
for(;money&>0;)
{
money-=2;
k++;
i++;
j++;
}
while(1)
{
if(i&>=2)
{
i-=2;
k++;
i++;
j++;
}
if(j&>=4)
{
j-=4;
i++;
j++;
k++;
}
if(i&<2j&<4)
break;
}
printf("%d %d %d",k,i,j);
printf("
");
return 0;
}
你們懂的!~~
最後喝多了也不知道幾瓶
不能借15瓶,能借20瓶。附python碼。
beer = 5
bottle = 0
cap = 0
count = 0
for i in range(20):
print "========",i,"======="
count += beer
print "drink: ",count
bottle += beer
cap += beer
beer = 0
beer = bottle/2
beer += cap/4
print "bottle: ",bottle
print "cap: ",cap
print "exchange",(bottle/2+cap/4)
bottle -= bottle/2*2
cap -= cap/4*4
print "left bottle: ",bottle
print "left cap: ",cap
print "when i can borrow beer:"
temp = 0
for i in range(1000):
# borrow i bottles of beer
if((cap+i)/4+(bottle+i)/2)&>=i:
temp = i
print "borrow beer: ",temp
count += temp
bottle += temp
cap += temp
# give back
bottle -= bottle/2*2
cap -= cap/4*4
print "drink more: ",temp
print "with borrowing left bottle: ",bottle
print "with borrowing left cap: ",cap
以及結果:
======== 0 =======
drink: 5
bottle: 5
cap: 5
exchange 3
left bottle: 1
left cap: 1
when i can borrow beer:
borrow beer: 3
drink more: 3
with borrowing left bottle: 0
with borrowing left cap: 0
======== 1 =======
drink: 11
bottle: 3
cap: 3
exchange 1
left bottle: 1
left cap: 3
when i can borrow beer:
borrow beer: 5
drink more: 5
with borrowing left bottle: 0
with borrowing left cap: 0
======== 2 =======
drink: 17
bottle: 1
cap: 1
exchange 0
left bottle: 1
left cap: 1
when i can borrow beer:
borrow beer: 3
drink more: 3
with borrowing left bottle: 0
with borrowing left cap: 0
======== 3 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 4 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 5 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 6 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 7 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 8 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 9 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
======== 10 =======
drink: 20
bottle: 0
cap: 0
exchange 0
left bottle: 0
left cap: 0
when i can borrow beer:
borrow beer: 0
drink more: 0
with borrowing left bottle: 0
with borrowing left cap: 0
讓我來用高中數學來解釋一下。
你買了一瓶啤酒,按照「4個蓋換1瓶,2個空瓶換1瓶」你可以換得"1/4+1/2"瓶酒,然後以換得的酒對應的瓶子和瓶蓋繼續換下去。
把上面的話理解好,「花1瓶的錢能通過兌換喝到幾瓶」這個問題轉化成了一個以1為首項,公比為3/4的等比數列求和,當n趨向於無限大,也就是你可以無限換下去時,Sn=a1/(1-q)=4。這就是說,在店鋪允許賒賬或者抵扣的前提下,每花1瓶的錢,你就可以喝4瓶酒。
至於這個答案具體是多少,已經不重要了。
大家都挺忙的,買菜的函數還沒有學好呢(認真臉)
運用物品價值理論:整瓶啤酒2元,2個瓶子換1瓶啤酒相當於瓶子1元,瓶蓋0.5元,那麼單純的啤酒就是0.5元 ,10/0.5=20瓶
1.換得5瓶,得到5個瓶蓋,5個空瓶
2.換得3瓶,還剩4個瓶蓋,4個空瓶
3.換得3瓶,還剩3個瓶蓋,3個空瓶
4.換得1瓶,還剩4個瓶蓋,2個空瓶
5.換得2瓶,還剩2個瓶蓋,2個空瓶
6.換得1瓶,還剩3個瓶蓋,1個空瓶
(借1瓶蓋 1空瓶)
7.換得2瓶,還剩2個瓶蓋,2個空瓶
(借2瓶蓋)
8.換得2瓶,還剩2個瓶蓋,2個空瓶
9.換得1瓶,還剩3個瓶蓋,1個空瓶
(還賬---&>結束)
總共可以喝20瓶
十元買五瓶,兩個空瓶換一瓶,四個蓋換一瓶,一個瓶價值一元對嗎,一個蓋價值0.5角,最後能喝17瓶還於一個瓶還有一個蓋呀
10/(2*(1-1/2-1/4)) = 20瓶
啥?借?不好意思,概不賒賬!
剛在群里看到這個問題,做出來後搜到此貼。 發現大家貼的都忒複雜了點,而且這麼點小事用得著搬紅菊花么……
一個方程和一張簡單的圖就搞定的事,何必那麼複雜。
所以說,數學還是挺有用的。
如果考慮那種允許一個空瓶加兩個瓶蓋來換的奇怪老闆,15再加兩瓶就好了。
15瓶,最後剩下一個空瓶,3個瓶蓋
大一剛學C,拿這個練練手
//用遞歸,這裡為了方便看懂用的中文變數
int 我的啤酒 = 10/2;
jisuan(ref 我的啤酒, 我的啤酒, 我的啤酒);
MessageBox.Show(我的啤酒.ToString());//運行結果15
private void jisuan(ref int 喝掉的啤酒, int 瓶蓋, int 瓶子)
{
if (瓶蓋&<4 瓶子&<2)
{
return;
}
int 兌換的啤酒 = 瓶蓋 / 4;
int 剩餘瓶蓋 = 瓶蓋 % 4;
兌換的啤酒 += 瓶子 / 2;
int 剩餘瓶子 = 瓶子 % 2;
剩餘瓶子 += 兌換的啤酒;//喝掉啤酒
剩餘瓶蓋 += 兌換的啤酒;
喝掉的啤酒 += 兌換的啤酒;
jisuan(ref 喝掉的啤酒, 剩餘瓶蓋, 剩餘瓶子);
從統籌演算法看是20瓶。從前後順序推演是15瓶
看到樓上C和Python的,我也來個JS版本(如果不能借的話):
function 來一炮(XO){
var X=parseInt(XO["剩下空瓶"]/2),O=parseInt(XO["剩下瓶蓋"]/4)
X || XO["剩下空瓶"]&<2 ? XO["剩下空瓶"]=XO["剩下空瓶"]%2+X+O : "喝完換空瓶"
O || XO["剩下瓶蓋"]&<4 ? XO["剩下瓶蓋"]=XO["剩下瓶蓋"]%4+X+O : "喝完換瓶蓋"
if(X+O){
XO["喝了"]+=X+O
return 來一炮(XO)
}else{
return XO
}
}
原諒我用中文,主要是便於理解,初始化10塊錢買了5瓶,所以:
對於我,來說兩瓶就倒。
醉了 人家問 10塊錢能喝幾瓶?為什麼?答:一瓶都不能喝。因為錢是不會喝酒的!
推薦閱讀:
※為什麼規定0!=1? 0的階乘不是應該是0么,為什麼教科書上規定是1,而且不給出任何理由。。。?
※「高等數學」這個名字是哪裡來的?
※求證所得圖形為正方形?
※是否存在非零整數數a,b,c,使ae+bπ+c=0?
※數學好是一種怎樣的體驗?
TAG:趣味數學 |