算法:10元可以喝几瓶啤酒?
题目:
啤酒2块钱1瓶,
4个瓶盖换1瓶
2个空瓶换1瓶
问:10块钱可以喝几瓶?
什么语言实现并不重要, 先要想好算法。然后在实现啊
=======================================================
gai + ping + jiu = 2, 4 * gai = 2, 2 * ping = 2, gai = 1/2, ping = 1, jiu = 1/2, 10 / jiu = 10 / (1/2) = 20
价值计算:
gai + ping + jiu = 2, 4 * gai = 2, 2 * ping = 2,
gai = 1/2, ping = 1, jiu = 1/2,
理想解:
10 / jiu = 10 / (1/2) = 20
瓶和盖不通用解:
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 1 * jiu + 3 * gai + 1 * ping + 1 * gai + 1 * ping
4 * gai + 2 * ping = 2 * jiu + 2 * gai + 2 * ping
2 * gai + 2 * ping = 1 * jiu + 2 * gai + 1 * gai + 1 * ping
3 * gai + 1 * ping = 0 * jiu + 3 * gai + 1 * ping
3 + 3 + 1 + 2 + 1 = 10 * jiu, 3 * gai + 1 * ping
瓶和盖通用解:
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 1 * jiu + 3 * gai + 1 * ping + 1 * gai + 1 * ping
4 * gai + 2 * ping = 2 * jiu + 2 * gai + 2 * ping
2 * gai + 2 * ping = 1 * jiu + 2 * gai + 1 * gai + 1 * ping
3 * gai + 1 * ping = 1 * jiu + 1 * gai + + 1 * gai + 1 * ping
2 * gai + 1 * ping = 1 * jiu + 1 * gai + 1 * ping
3 + 3 + 1 + 2 + 1 + 1 + 1 = 12 jiu, 1 * gai + 1 * ping
----------------------------------------------------------------------------------
价值计算:
gai + ping + jiu = 2, 4 * gai = 2, 2 * ping = 2,
gai = 1/2, ping = 1, jiu = 1/2,
超级理想解:
10 / jiu = 10 / (1/2) = 20
瓶和盖通用解: 瓶和盖不通用情况的,符合实际的(最后买一瓶,最后剩1瓶1盖) 理想解
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 1 * jiu + 3 * gai + 1 * ping + 1 * gai + 1 * ping
4 * gai + 2 * ping = 2 * jiu + 2 * gai + 2 * ping
2 * gai + 2 * ping = 1 * jiu + 2 * gai + 1 * gai + 1 * ping
3 * gai + 1 * ping = 1 * jiu + 1 * gai + + 1 * gai + 1 * ping
2 * gai + 1 * ping = 1 * jiu + 1 * gai + 1 * ping
3 + 3 + 1 + 2 + 1 + 1 + 1 = 12 jiu, 1 * gai + 1 * ping
瓶和盖不通用解:
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 1 * jiu + 3 * gai + 1 * ping + 1 * gai + 1 * ping
4 * gai + 2 * ping = 2 * jiu + 2 * gai + 2 * ping
2 * gai + 2 * ping = 1 * jiu + 2 * gai + 1 * gai + 1 * ping
3 * gai + 1 * ping = 0 * jiu + 3 * gai + 1 * ping
3 + 3 + 1 + 2 + 1 = 10 * jiu, 3 * gai + 1 * ping
现在问题来了,如何在瓶和盖不通用的情况下达到理想解
----------------------------------------------------------------------------------
价值计算:
gai + ping + jiu = 2,
4 * gai = 2, 2 * ping = 2,
gai = 1/2, ping = 1,
jiu = 2 - 1/2 - 1 = 1/2,
超级理想解:
10 / jiu = 10 / (1/2) = 20
瓶和盖通用解: 瓶和盖不通用情况的,符合实际的(最后买一瓶,最后剩1瓶1盖) 理想解
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping = 4 * gai + 4 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 2 * jiu + 1 * gai + 2 * gai + 2 * ping = 3 * gai + 2 * ping
3 * gai + 2 * ping = 1 * jiu + 3 * gai + 1 * gai + 1 * ping = 4 * gai + 1 * ping
4 * gai + 1 * ping = 1 * jiu + 1 * ping + 1 * gai + 1 * ping = 1 * gai + 2 * ping
1 * gai + 2 * ping = 1 * jiu + 1 * gai + 1 * gai + 1 * ping = 2 * gai + 1 * ping
2 * gai + 1 * ping = 1 * jiu + 1 * gai + 1 * ping
5 + 3 + 3 + 2 + 1 + 1 + 1 + 1 = 17 * jiu, 1 * gai + 1 * ping
验证:17 * 1/2 + 1 * 1/2 + 1 * 1 = 10 == 10
瓶和盖不通用解:
10 = 5 * jiu + 5 * gai + 5 * ping
5 * gai + 5 * ping = 3 * jiu + 1 * gai + 1 * ping + 3 * gai + 3 * ping
4 * gai + 4 * ping = 3 * jiu + 3 * gai + 3 * ping
3 * gai + 3 * ping = 1 * jiu + 3 * gai + 1 * ping + 1 * gai + 1 * ping
4 * gai + 2 * ping = 2 * jiu + 2 * gai + 2 * ping
2 * gai + 2 * ping = 1 * jiu + 2 * gai + 1 * gai + 1 * ping
3 * gai + 1 * ping = 0 * jiu + 3 * gai + 1 * ping
5 + 3 + 3 + 1 + 2 + 1 = 15 * jiu, 3 * gai + 1 * ping
验证:15 * 1/2 + 3 * 1/2 + 1 * 1 = 10 == 10
现在问题来了,如何在瓶和盖不通用的情况下达到理想解
=================================================================================
下面是一个网友用汇编做的题:
assume cs:cao,ds:data
data segment
kp dw 2 ;瓶
gai dw 4 ;盖
qian db 10 ; 钱
ping dw 0 ;总瓶数
data ends
cao segment
s:mov ax,data
mov ds,ax
mov dx,0 ; DX为盖子累加数
mov di,0 ;di为瓶累加数
mov cl,qian ;钱数
s0:sub cl,2 ;每次一瓶就是2元
inc ping ;总瓶数加1
mov si,ping ;SI为间接变量
sub si,di ;总瓶数-瓶子累加等于真值
cmp si,kp ;真值对比是否大于2
ja ok ;大于跳
kk:mov si,ping ; SI为间接变量
sub si,dx ;总盖子-累加盖子等于真值
cmp si,gai ;真值对比是否大于4
jcxz jiesu ;钱是否为0
jb s0 ;盖子是否小于,否不满足加瓶数条件
inc ping ;满足条件
add dx,4 ;累加盖子加4
jmp s0 ;循环
jiesu:mov ax,4c00h ;退出
int 21h
ok:inc ping ;总瓶子累加
add di,2 ;瓶子记录
jmp kk
cao ends
end s
出处:QQ群:编程算法&思想(459909287)
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/5030895.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2015-12-08 20:33 jack_Meng 阅读(14019) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)