2018-09-15

2018-09-15  本文已影响0人  木马音响积木

今天 做 pat ,陈越老师的,数据结构 第二题

得到的结果

image.png

最大N 什么意思,我在代码中,防备了,大于100000 的情况。
各路大侠,请指点我。。

题目是


image.png

我的python 代码

import math


a1 =[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,\
109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,\
227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317]

#print(len(a1))

#x=3675
#ttlist = [a for a in a1 if a<= (math.floor(math.sqrt(x)))]
#print(ttlist)
#tt=[num for num in range(319,500,2)]
#print(tt)

# if the x like 13 ,return true ,else false
# shai zi suanfa
def issusu( x ):
    ttlist = [a for a in a1 if a<= (math.floor(math.sqrt(x)))]
    for i in ttlist:
        if x%i ==0: return False    
    return True
    
def findx( a ,x):
    ff=0
    templist = [g for g in a if g<= x]
    for i in range(0,len(templist)-1):
        if (templist[i+1]-templist[i])==2:
            ff+=1
    return ff
    
def come(k):
    listkk=[]
    # return the primes list,maybe  start 331
    tt=[num for num in range(319,k+1,2)]
    for i in tt:
        if issusu(i):listkk.append(i)
    return listkk

def lala():    
    #strn='19999'
    #strn='20'
    strn=input()
    # except case ,if input is r
    #n=int(strn)
    
    #strn = input("please input the Integer,like 33,limit is  from 1 to 99999:")
    
    try:
        n=int(strn)
        if n>100000 : print(0)
        #print(n)
    except ValueError:    
        print("input is not a good Integer!")

    if 1<=n<=318 :
        x1=findx(a1,n)
        print(x1)       #done
    elif (n>318 and n<=100000):
        templist =[]
        templist =come(n)
        x1=findx(a1,318)
        x2=findx(templist,n)
    #becaust we known 313,317 ,331,337 ,(331-317) >2
        x3=x1+x2
        
        print(x3)    #done 
    else :
        print(0)

lala()

上一篇 下一篇

猜你喜欢

热点阅读