Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环中出现错误。(寻找三个整数)_Python_For Loop - Fatal编程技术网

Python for循环中出现错误。(寻找三个整数)

Python for循环中出现错误。(寻找三个整数),python,for-loop,Python,For Loop,所以,我们的老师给我们布置了一个任务,让我们找出三个整数a,bc。使用Python时,它们的大小都在0到450之间 如果b为偶数,则a=c+11 如果b为奇数,则a=2c-129 b=ac mod 2377 c=(∑(b-7k)从k=0到a-1)+142(编辑。我写错了。Was-149) 我厌倦了这样的代码:(还是个新手。我想我的很多代码都是错的) 但我得到了一个错误: for a, b, c in range(0, 450): TypeError: 'int' object is not it

所以,我们的老师给我们布置了一个任务,让我们找出三个整数a,bc。使用Python时,它们的大小都在0到450之间

如果b为偶数,则a=c+11 如果b为奇数,则a=2c-129

b=ac mod 2377

c=(∑(b-7k)从k=0到a-1)+142(编辑。我写错了。Was-149)

我厌倦了这样的代码:(还是个新手。我想我的很多代码都是错的)

但我得到了一个错误:

for a, b, c in range(0, 450):
TypeError: 'int' object is not iterable

我做错了什么?如何让它检查0到450之间的每个数字

您需要嵌套循环,以像尝试一样对其进行暴力:

for a in range(451): # range(450) excludes 450
    for b in range(451):
        for c in range(451):
            ...

很明显是O(n3),但如果你想要一个快速而肮脏的答案,我想最坏的情况下,它只能工作9100万个循环。

Nick T和Eric的答案希望通过迭代
a
b
c
的值帮助你解决问题。我还想指出,你处理这个问题的方式是行不通的。如果要在循环的每次迭代中将
a
重新分配给某个对象,那么迭代
a
的各种值有什么意义?同样,对于
b
c
。更好的方法是检查任何给定的三元组
(A,b,c)
是否满足赋值中给定的条件。例如:

import itertools

for b, c in itertools.product(*[range(450)]*2):
    if b % 2 == 0:
        a = c + 11
    else:
        a = 2 * c - 129
    derived_b = (a * c) % 2377
    derived_c = sum(b - 7 * k for k in range(0, a - 1))

    if derived_b == b and derived_c == c:
        print a, b, c
from itertools import product, tee

def test(a, b, c):
    flags = {'a': False,
             'b': False,
             'c': False}
    if (b % 2 == 0 and a == c+11) or (b % 2 == 1 and a == 2*c-129):
        flags['a'] = True
    if b == (a * c) % 2377:
        flags['b'] = True
    if c == sum(b - 7*k for k in range(a-1)) - 149:
        flags['c'] = True
    return all(flags.values()) # True if zero flags are False

def run_tests():
    # iterate over all combinations of a=0..450, b=0..450, c=0..450
    for a, b, c in product(*tee(range(451), 3)):
        if test(a, b, c):
            return (a, b, c)

print(run_tests())

注意:这是一个缓慢的解决方案。像GLGL的回答或邓肯的评论那样,循环次数较少的方法显然是有利的。这实际上更多的是为了说明目的

带有[0450]的内容只是一个提示

事实上,变量是耦合在一起的。您可以立即直接消除至少一个循环:

for b in range(0, 451):
    for c in range(0, 451):
        if b % 2: # odd
            a = 2 * c - 129
        else:
            a = c + 11
        if b != (a * c) % 2377: continue # test failed
        if c != sum(b - 7 * k for k in range(a)): continue # test failed as well
        print a, b, c
应该做这项工作。

我不会发布完整的代码(毕竟,这是家庭作业),但您可以消除两个外部循环。如果在
c
上迭代,这是最简单的

然后,您的代码应该如下所示:

 for c in range(451):
     # calculate a assuming b is even
     # calculate b
     # if b is even and a and b are in range:
        # calculate what c should be and compare against what it is
     # calculate a assuming b is odd
     # calculate b
     # if b is odd and a and b are in range:
        # calculate what c should be and compare against what it is
消除代码重复以计算
c

a = c + 11 if b is even
a = 2c-129 if b is odd
b = ac mod 2377

c = (∑(b-7k) from k = 0 to a-1) +142
这使您在所有3个数字之间有一个很强的关系

给定一个值
a
,有两个值
c
a-11
(a+129)/2
),这两个值依次为b提供了两个值(
ac mod 2377
,条件是b的结果很奇怪),这两个值依次应用于验证c的公式中

由于要计算c的公式,所以这个问题的总体复杂度是o(n^2)

下面是一个实现示例:

for a in xrange(451):
    c_even = a - 11
    b = (a*c_even) % 2377
    if b % 2 == 0:
        c = sum(b - 7 * k for k in range(a)) + 142
        if c == c_even:
            print (a, b, c)
            break
    c_odd = (a+129)/2
    b = (a*c_odd) % 2377
    if b % 2 == 1:
        c = sum(b - 7 * k for k in range(a)) + 142
        if c == c_odd:
            print (a, b, c)
            break

在这种情况下,您希望它做什么?范围(0450)为您提供一个单一的数字,而不是一次3。我认为您需要迭代1个值(可能是a)并验证其他2个值。在0到450之间有超过9100万个数字组合。这需要测试很多,尤其是当计算
c
涉及到另一个循环时。最好只在该范围内迭代一个数字,然后从中计算其他两个数字,然后检查所有数字是否都在所需范围内,以及第三个数字的计算值是否与开始时的数字匹配。这是451个循环,而不是9100万个循环。顺便说一句,您对
c
的计算有两个错误:范围
参数错误,您忘记添加
142
。我恨你用暴力手段后发现的。我编写的解决方案(但我不会破坏它)花了96毫秒找到唯一的解决方案,所以有一个目标为你。什么是“产品”和“t”?@Zahand请看。超过9100万:请记住,计算
c
的正确值平均需要225次迭代。除非跳过一些循环,否则这个循环是21亿次,所以没有暴力强迫。方程式的格式有点混乱,所以我就把它抛到了“为什么我不能从一个数字中解包三个数字”的问题上。nˆ4实际上是因为c的计算
for a in xrange(451):
    c_even = a - 11
    b = (a*c_even) % 2377
    if b % 2 == 0:
        c = sum(b - 7 * k for k in range(a)) + 142
        if c == c_even:
            print (a, b, c)
            break
    c_odd = (a+129)/2
    b = (a*c_odd) % 2377
    if b % 2 == 1:
        c = sum(b - 7 * k for k in range(a)) + 142
        if c == c_odd:
            print (a, b, c)
            break