1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 麻省理工学院公开课:计算机科学及编程导论习题2

麻省理工学院公开课:计算机科学及编程导论习题2

时间:2019-08-24 16:03:39

相关推荐

麻省理工学院公开课:计算机科学及编程导论习题2

习题1:

已知6a + 9b + 20c = n,当n =50, 51, 52,53, 54, 55时,a、b、c有自然数解(我不知道现在是怎么定义的,但我以前学的时候自然数包括0),

如何求出n = 56~65时,a、b、c的自然数解。

如果直接求56~65的解,只要穷举就可以了:

def eq(x):x = int(x)y = []for a in range(0, x / 6 + 1):for b in range(0, x / 9 + 1):for c in range(0, x / 20 + 1):if 6 * a + 9 * b + 20 * c == x:y.append([a, b, c])return yn = input()print eq(n)

因为56-50=6,

57-51=6,

58-52=6,

59-53=6,

.

.

.

.

65-59=6。 6a+9b+20c=n,只要在之前的解的基础上a+1就可以了,因为6(a+1)=6a+6。 类似还有相差9、20

定理:存在一个x,如果n=x、n=x+1、...、n=x+5有自然数解,那么当n>=x时,一定有自然数解。

习题2:

为什么这个定理是正确的。

就如上面所说的原因:

6a + 9b + 20c = x

6a + 9b + 20c = x+1

6a + 9b + 20c = x+2

6a + 9b + 20c = x+3

6a + 9b + 20c = x+4

6a + 9b + 20c = x+5

有自然数解。

a = d - 1,因为a是自然数,那么d = a+1 也是自然数。

6(d-1) + 9b + 20c = x-------> 6d + 9b + 20c = x+6

6(d-1)+ 9b + 20c = x+1 -------> 6d + 9b + 20c = x+7

6(d-1) + 9b + 20c = x+2 -------> 6d + 9b + 20c = x+8

6(d-1)+ 9b + 20c = x+3 -------> 6d + 9b + 20c = x+9

6(d-1)+ 9b + 20c = x+4 -------> 6d + 9b + 20c = x+10

6(d-1) + 9b + 20c = x+5 -------> 6d + 9b + 20c = x+11

至于数学上怎么证明,我又不是数学系。

习题3:

用循环编程写出最大没有自然数解的n。结果是43。

def eq(x):x = int(x)y = []for a in range(0, x / 6 + 1):for b in range(0, x / 9 + 1):for c in range(0, x / 20 + 1):if 6 * a + 9 * b + 20 * c == x:y.append([a, b, c])return ydef check(x):if len(eq(x)) != 0:return Trueelse:return Falsedef check6(x):if check(x) and check(x+1) and check(x+2) and check(x+3) and check(x+4) and check(x+5):return Trueelse:return Falsei = 6while not(check6(i)):i = i + 1print "Largest number of McNuggets that cannot be bought in exact quantity: %r" % (i-1)

并不是最快的,因为会计算每一个解。

def check(x):x = int(x)count = 0for a in range(0, x / 6 + 1):for b in range(0, x / 9 + 1):for c in range(0, x / 20 + 1):if 6 * a + 9 * b + 20 * c == x:count = count + 1if count > 0:return Truebreakif count == 0:return Falsedef check6(x):if check(x) and check(x+1) and check(x+2) and check(x+3) and check(x+4) and check(x+5):return Trueelse:return Falsei = 6while not(check6(i)):i = i + 1print "Largest number of McNuggets that cannot be bought in exact quantity: %r" % (i-1)

习题4:

xa + yb + zc = n,package = (x, y ,z),此元组要求从小到大,求出最大没有自然数解的n。

输入不同的x,y,z;包括(6,9,20)。只要把上面的编码稍微改下就好。

def check(n, x, y, z):n = int(n)count = 0for a in range(0, n / x + 1):for b in range(0, n / y + 1):for c in range(0, n / z + 1):if x * a + y * b + z * c == n:count = count + 1if count > 0:return Truebreakif count ==0:return Falsedef check6(n, x, y, z):if check(n, x, y, z) and check((n + 1), x, y, z) and check((n + 2), x, y, z) and check((n + 3), x, y, z) and check((n + 4), x, y, z) and check((n + 5), x, y, z):return Trueelse:return Falseprint "Please enter three number for x, y, z in acending order."x = int(input())y = int(input())z = int(input())package = (x, y, z)package = sorted(package)i = package[0]j = package[1]k = package[2]n = 0while not(check6(n, i, j, k)):n = n + 1print "Given package sizes %r, %r, and %r, the largest number of McNuggets that cannot be bought in exact quantity is: %r" % (i, j, k, (n-1))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。