py3 46分求助
查看原帖
py3 46分求助
712514
Calculatehate楼主2023/1/6 16:35
import math
def sushu(x):
    if x == 2:
        return True
    elif x >= 3:
        s = 0
        for i in range(2,int(math.sqrt(x))+1):
            if x % i == 0:
                s = s + 1
        if s == 0:
            return True
        else:
            return False
L,R = map(int,input().split())
ans = 0
for i in range(L,R+1):
    if sushu(i):
        ans = ans + 1
print(ans)

------------
46#1-#4,#11 AC  else:TLE

[](https://blog.csdn.net/cpyname/article/details/75126312)
后采用方法2如下:
import math  
def judgetwo(x):
    s = len(x)
    ans = 0
    for i in range(0,s - 1):
        if x[i] % 2 == 0:
            ans = ans + 1
    if ans >= 1:
        return True
    else:
        return False        
lista = []
L,R = map(int,input().split())
for i in range(L,R + 1):
    lista.append(i)
while judgetwo(lista):
    long = len(lista)
    for j in range(0,long - 1):
        if lista[j] % 2 == 0:
            lista.remove(lista[j])
def judgeshou(x):
    n = list[0]
    s = len(x)
    ans = 0
    for i in range(0,s - 1):
        if x[i] % n == 0:
            ans = ans + 1
    if ans >= 1:
        return True
    else:
        return False
while judgeshow(lista):
    long = len(lista)
    for k in range(0,long - 1):
        if lista[k] % int(lista[k]) == 0:
            lista.remove(lista[k])
print(len(lista))

报错:Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python310/123456.py", line 19, in <module>
    if lista[j] % 2 == 0:
IndexError: list index out of range

列表掌握的不是很好。请问大神们怎么改


2023/1/6 16:35
加载中...