2014-03-19 06:40
题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药。给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的?
解法:如果药片管够,从每个瓶子里取出数量各不相同的药片,根据质量的总和减去“期望的”质量总和,就知道哪瓶有问题了。
代码:
1 // 6.1 There are 20 bottles of pills, all of which have pills of 1g, except one with 1.1g. 2 // Given a balance that can provides exact measurement, can you devise a method to find out the special bottle with just one try? 3 // Answer: 4 // If you pick n[i] pills from bottle i, the total weight should be sigma(n[i] * 1.0). 5 // But there is one with 1.1g pills, that would cause a little deviation to the sum. 6 // For example, if we pick i pills from bottle i, the sum should be (20 + 1) * 20 / 2 = 210. 7 // The real weight w should be greater than 210, (w - 210) / (1.1 - 1.0) will be the number. 8 int main() 9 { 10 return 0; 11 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)