Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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
转换C++;for循环python_Python_C++_Loops - Fatal编程技术网

转换C++;for循环python

转换C++;for循环python,python,c++,loops,Python,C++,Loops,尝试四处搜索类似的问题,但无法解决此问题。不知道如何正确地转换C++循环中的一些特性。尤其是main()中的count

尝试四处搜索类似的问题,但无法解决此问题。不知道如何正确地转换C++循环中的一些特性。尤其是
main()
中的
count<20
条件

原始问题:

#包括
整数计数因子(整数n){
如果(n<2)返回1;
int count=2;//1和n

对于(inti=2;i你可以试试这个。这会产生你想要的结果吗

def countDivisors(n):
    if (n < 2):
        return 1
    count = 2

    for i in range(2, int(n/2)-1, 1):
        print(i)    
        if(n%i == 0):
            count = count + 1
    return count

def main():
        print("The first 20 anti-primes are: ")
        n,maxDiv = 1,0
        for d in range(1,20):    
            d = countDivisors(n)
            print(d)
            if(d > maxDiv):
                maxDiv = d
            n+=1
        return 0
if __name__ == "__main__":
    main()
def计数因子(n):
如果(n<2):
返回1
计数=2
对于范围(2,int(n/2)-1,1)内的i:
印刷品(一)
如果(n%i==0):
计数=计数+1
返回计数
def main():
打印(“前20个反素数为:”)
n、 maxDiv=1,0
对于范围(1,20)内的d:
d=计数因子(n)
印刷品(d)
如果(d>maxDiv):
maxDiv=d
n+=1
返回0
如果名称=“\uuuuu main\uuuuuuuu”:
main()

严格来说,对于
循环的两个

代码中的
int(n/2)-1
将是C中的
i

20一号,


基本上,C
for
循环和Python
for
循环是不同的

第一个选择是按照您所做的那样,使用Python
while
循环来复制测试并停止C
for
循环的一部分。如果您这样做,您必须自己编写迭代

第二种选择是使用Python
for
循环来复制迭代,并写出测试和停止部分代码

import itertools
.
.
.
        for n in itertools.count(1):    
            d = countDivisors(n)

            if(d > maxDiv):
                print(n)
                maxDiv = d
                count += 1
                if count >= 20:
                    break

我想我更喜欢这种形式,但这在很大程度上取决于我的品味。

我在
计数因子中使用map函数

def countDivisors(n):
    if n < 2:
        return 1
    return 2 + sum(map(lambda x: 0 if n % x else 1, range(2, int(n / 2) + 1)))


def main():
    max_div = 0
    count = 0
    print("The first 20 anti-primes are:")
    x = 1
    while count < 20:
        d = countDivisors(x)
        if d > max_div:
            print(x, end=' ')
            max_div = d
            count += 1
        x += 1
    return 0


if __name__ == '__main__':
    main()

MyDIV太早了。MXDIV的初始值是0。我实际上初始化了Max DIV并计数为0,但是在我发布之前,我把它删除了。对原始问题做了一些编辑。最好在编写Python代码时避免在C++中思考。逻辑可能是逻辑,但是如何最好地实现这两种语言的逻辑是非常非常不同的。试过了,没能从盒子里出来。我稍后再看一下,看看有没有小的改动可以解决这个问题。加上它的疑问,还有原来的问题,你的第二个循环的翻译是错误的。C++中的原始循环会循环20次以上,而当<代码>计数< /代码>时,不打破代码> i <代码>已经达到20。@ MartinBonnersupportsMonica true,我当时正专注于另一个循环。@StormClaw:我不想再编辑它,但
n+=1
部分是“自动更正”,没有注释:所以在问题中它“太缩进了”这是
if
的一部分,而不是
while
的一部分。然后它就起作用了,看,我正试图避免使用库,这就是为什么我要使用while方法。现在已经很晚了,所以我明天将对此进行另一次尝试。也许到时候会有人想出解决方案。
for i in range(2,int(n/2)+1):
int maxDiv = 0, count = 0;    // <-- !
for (int n = 1; count < 20; ++n) {
    int d = countDivisors(n);
    if (d > maxDiv) {
        std::cout << n << " ";
        maxDiv = d;
        count++;
    }
maxDiv = 0  # <-- !
count = 0   # <-- !
n = 1
while count < 20:    
    d = countDivisors(n)
    if(d > maxDiv):
        print(n)
        maxDiv = d
        count += 1
    n += 1
import itertools
.
.
.
        for n in itertools.count(1):    
            d = countDivisors(n)

            if(d > maxDiv):
                print(n)
                maxDiv = d
                count += 1
                if count >= 20:
                    break
def countDivisors(n):
    if n < 2:
        return 1
    return 2 + sum(map(lambda x: 0 if n % x else 1, range(2, int(n / 2) + 1)))


def main():
    max_div = 0
    count = 0
    print("The first 20 anti-primes are:")
    x = 1
    while count < 20:
        d = countDivisors(x)
        if d > max_div:
            print(x, end=' ')
            max_div = d
            count += 1
        x += 1
    return 0


if __name__ == '__main__':
    main()
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560