题目是这样的:
12个球,其中一个质量和其余的不同,用一个无砝码的天平最多分3次称出。以前考虑过,但最终得出了一个错误的结论。这次应 quanben 之请再想了一下,大致如下解法:
# vim: set sts=4 ts=4 sw=4 et:
# twlvball.py: solves the twelve balls problem.
import sys
import operator
class Ball():
def __init__(self, num, w):
self.num = num
self.weight = w
def main():
print "This program is fragile. Try not to enter wrong arguments."
sys.stdout.write("ball number[0-11]: ")
num = int(sys.stdin.readline().rstrip())
assert 0 <= num and num < 12
sys.stdout.write("weight[h/l]: ")
w = sys.stdin.readline().rstrip()
assert w in ["h", "l"]
test(num, w)
def test(num, w):
balls = []
for i in range(12):
12个球,其中一个质量和其余的不同,用一个无砝码的天平最多分3次称出。以前考虑过,但最终得出了一个错误的结论。这次应 quanben 之请再想了一下,大致如下解法:
# vim: set sts=4 ts=4 sw=4 et:
# twlvball.py: solves the twelve balls problem.
import sys
import operator
class Ball():
def __init__(self, num, w):
self.num = num
self.weight = w
def main():
print "This program is fragile. Try not to enter wrong arguments."
sys.stdout.write("ball number[0-11]: ")
num = int(sys.stdin.readline().rstrip())
assert 0 <= num and num < 12
sys.stdout.write("weight[h/l]: ")
w = sys.stdin.readline().rstrip()
assert w in ["h", "l"]
test(num, w)
def test(num, w):
balls = []
for i in range(12):