발견 된 소수를 계산하여 결과를 Wolfram Alpha의 결과와 비교하려고 시도했습니다.파이썬의 SPOJ PRIME1에 대한 잘못된 대답
오류없이 잘 작동하는 것 같습니다.
그러나이 솔루션을 SPOJ에 제출하면 오류 메시지 "잘못된 대답"이 표시됩니다.
또한 최종 인쇄물의 끝을 ''(빈 문자열)로 변경하려고했지만 여전히 "잘못된 대답"이 표시됩니다.
체 알고리즘이나 출력 오류에 문제가 있는지 확실하지 않습니다.
** 편집 : 문제 http://www.spoj.pl/problems/PRIME1/ 여기
내 PRIME1 소스 코드가의 링크를 누군가가 내 잘못을 지적 할 수 있기를 바랍니다. 고마워.
누군가가 나에게이 같은 프로그램에 테스트를 작성하는 법을 가르쳐 주길 바란다. 나는 여전히 배우고 있으며, 프로그램에 자동화 된 테스트를 수행하는 법을 알고 있지만 배우고 싶다.
def getPrimeInRange(minNum, maxNum):
#just a variation with skipping step of the Sieve of E's
processingRange = list(range(minNum, maxNum+1))
#prefix, due to 1 is not a prime
if minNum == 1:
processingRange[0] = 0
sqrtOfMaxNum = int(maxNum ** 0.5) + 1
primesUnderSqrt = list(range(sqrtOfMaxNum))
#prefix, due to 1 is not a prime
primesUnderSqrt[1] = 0
#my strategy is flip all numbers that is not a prime to zero, which equals to False.
#for Sieve of E's, all the primes under sqrt of max num are the only needed numbers to sieve primes out.
#so here is a smaller Sieve of E's for numbers under sqrt
for n in primesUnderSqrt:
if n: #which equals to "if n != 0"
nowIndex = n + n
while True:
try:
primesUnderSqrt[nowIndex] = 0
nowIndex += n
except IndexError:
break
#full aspect sieve
for n in primesUnderSqrt:
if n:
#for easier flip processing, I need the offset number for the flipping.
nMultipleMostCloseToMin = n * (minNum // n)
if nMultipleMostCloseToMin == minNum:
nowIndex = 0
elif sqrtOfMaxNum <= minNum:
nowIndex = nMultipleMostCloseToMin + n - minNum
elif sqrtOfMaxNum > minNum:
nowIndex = nMultipleMostCloseToMin + n - minNum + n
#happy flippin'
while True:
try:
processingRange[nowIndex] = 0
nowIndex += n
except IndexError:
break
return processingRange
def main():
todoTimes = int(input())
todoNums = list(range(todoTimes))
stringOutput = ''
for i in range(todoTimes):
todoNums[i] = input().split()
todoNums[i][0] = int(todoNums[i][0])
todoNums[i][1] = int(todoNums[i][1])
for [minNum, maxNum] in todoNums:
#countedNum = 0 #for algo debugging
for n in getPrimeInRange(minNum, maxNum):
if n:
stringOutput += str(n)
#countedNum += 1 #for algo debugging
stringOutput += '\n'
stringOutput += '\n'
#stringOutput += str(countedNum) #for algo debugging
stringOutput = stringOutput.rstrip('\n')
print(stringOutput)
ifMainSucceed = main()
고마워요! 나는이 조건에서 어색한 것을 느낀다. 내 뇌가 꼬여서 놀란다. – Shem
실례합니다. elif 조건을 n <= minNum으로 변경하고 두 번째 elif를 다른 것으로 변경 한 후에도 여전히 잘못된 대답을 얻었습니다 .... – Shem
the primes 생성 된 코드가 변경된 것처럼 보이기 때문에 SPOJ는 잘못된 답을 제공하지 않습니다. – Shem