oj质数个数

14 views
Skip to first unread message

1600015496

unread,
Jan 1, 2019, 10:39:29 AM1/1/19
to cs101pku
题目链接:
求问下面的代码哪里错了 
我的代码:
import math

def isPrime(n):
	if n <= 1:
		return False
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

T=int(input())
for i in range(T):
    start,end=[int(x) for x in input().split()]
    output=[]
    for y in range(start,end+1):
        if isPrime(y)==1:
            output.append(y)
    if len(output)!=0:
        print(' '.join(str(output)))        
    else:
        print('-1')


老师的代码:


import math

def isPrime(n):

  if n <= 1:

  return False

  for i in range(2, int(math.sqrt(n)) + 1):

  if n % i == 0:

  return False

  return True

T = int(input())

while T!=0:

  T -= 1

  ans = []

  start, end = map(int, input().split())

  for i in range(start, end+1):

  if isPrime(i):

  ans.append(i)

  if len(ans) != 0:

  print(' '.join(map(str, ans)))

  else:

  print(-1)



发自网易邮箱大师

俞锦源

unread,
Jan 1, 2019, 10:58:42 AM1/1/19
to cs101pku
目测是输出的位置
if len(output)!=0:
        print(' '.join(str(output)))   
改成:
if len(output)!=0:
        print(' '.join([str(x) for x in output])   
在 2019年1月1日星期二 UTC+8下午11:39:29,1600015496写道:

1600015496

unread,
Jan 1, 2019, 11:03:50 AM1/1/19
to cs10...@googlegroups.com, cs101pku
对的,改过来过后已经ac了,但我试了一种,
 for i in output:
            print(i,end=' ')        

这种最后显示presentation error,是为什么呢? (因为输出的最后多了一个空格吗?)
发自网易邮箱大师

--
您收到此邮件是因为您订阅了Google网上论坛上的“cs101pku”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到cs101pku+u...@googlegroups.com
要发帖到此群组,请发送电子邮件至cs10...@googlegroups.com
访问此群组:https://groups.google.com/group/cs101pku
要查看更多选项,请访问https://groups.google.com/d/optout

俞锦源

unread,
Jan 1, 2019, 11:25:26 AM1/1/19
to cs10...@googlegroups.com
应该是多空格hhh 我记得这种输出在cf上有用 在oj上就用不了


-----原始邮件-----
发件人:1600015496 <16000...@pku.edu.cn>
发送时间:2019-01-02 00:03:13 (星期三)
收件人: "cs10...@googlegroups.com" <cs10...@googlegroups.com>
抄送: cs101pku <cs10...@googlegroups.com>
主题: 回复: oj质数个数

1600015496

unread,
Jan 1, 2019, 11:25:56 AM1/1/19
to cs10...@googlegroups.com, cs10...@googlegroups.com
好的 感谢~


发自网易邮箱大师

Reply all
Reply to author
Forward
0 new messages