[CareerCup] 6.1 Find Heavy Bottle 寻找重瓶子

简介:

6.1 You have 20 bottles of pills. 19 bottles have 1.0 gram pills, but one has pills of weight 1.1 grams. Given a scale that provides an exact measurement, how would you find the heavy bottle? You can only use the scale once.

这道题是一道智力题,给我们20个瓶子,里面分别装满了药片,其中有19个瓶子都装了1克的药品,有一个瓶子装了1.1克的药片,我们有一个称,可以精确的称重,但是只允许用一次,让我们找出中的那个。

这题我没想出来,我开始纠结于整瓶称,其实这题可以取若干药品出来称。我们先从简单的开始想,如果只有两瓶,那我们随便从其中一瓶中取一粒出来一称就知道了,那如果有三瓶呢,我们怎么办呢。答案是从第一瓶中取一粒,第二瓶中取两粒,第三瓶中取三粒来称,总共取出了六粒,如果六粒都是一克的话,那么称出来是六克,但是有一瓶是每粒药片1.1克,那么如果第一瓶是较重那瓶,称出来以为6.1克,如果第二瓶较重,则为6.2克,同理若第三瓶较重,则为6.3克,那么这样我们就能根据结果来知道哪瓶较重了。

同理对于20瓶也是一样的,我们从第一瓶取一粒,第二瓶取两粒,第三瓶取三粒,以此类推,第二十瓶取二十粒,如果每瓶都一样重,那称出来应该是210克,那有一瓶中的每粒药片是1.1克,我们根据个数差产生的重量差来确定是哪瓶,计算公式为:(weight - 210) / 0.1

本文转自博客园Grandyang的博客,原文链接:寻找重瓶子[CareerCup] 6.1 Find Heavy Bottle ,如需转载请自行联系原博主。

相关文章
|
10月前
杭电2095(find your present (2))
杭电2095(find your present (2))
53 0
UVa11958 - Coming Home
UVa11958 - Coming Home
79 0
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1128 N Queens Puzzle
【PAT甲级 - C++题解】1128 N Queens Puzzle
83 1
|
机器学习/深度学习
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
162 0
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
125 0
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
AtCoder Beginner Contest 214 D.Sum of Maximum Weights (思维 并查集)
130 0
|
人工智能 vr&ar
Atcoder--Candy Distribution II--前缀和+map
题目描述 There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l,r) that satisfy the following:
118 0
【1128】N Queens Puzzle (20分)【逻辑题】
【1128】N Queens Puzzle (20分)【逻辑题】 【1128】N Queens Puzzle (20分)【逻辑题】
126 0
|
算法 Python
动态规划法(六)鸡蛋掉落问题(一)(egg dropping problem)
  继续讲故事~~   这天,丁丁正走在路上,欣赏着路边迷人的城市风景,突然发现前面的大楼前围了一波吃瓜群众。他好奇地凑上前去,想一探究竟,看看到底发生了什么事情。
1986 0
1128. N Queens Puzzle (20) 判断是否是对角线
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other.
1147 0