亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何只打印此輸出的最后一行?

如何只打印此輸出的最后一行?

LEATH 2023-08-03 17:30:14
我需要找到 10000 以下的最長的非素數行。如何僅打印輸出中的最后一行?沒有 max 函數,也沒有簡單的數值約束。我認為它只需要一些小的調整,只是不知道在哪里或如何調整。priemen = []for x in range(2,10000):    #prime generator    tel = 0    for deler in range(1,x):        if x % deler == 0:            tel += x % deler == 0    if tel <2:        priemen.append(x)   a = priemen[0]b = priemen[1]maxrow = 0for next in priemen[2:]:        a = b    b = next    row = b - a - 1               if row > maxrow:        maxrow = row        print("The longest row starts at", a+1, "and stops at", b-1, "and is", maxrow, "long.")------------------Output: The longest row starts at 8 and stops at 10 and is 3 long.The longest row starts at 24 and stops at 28 and is 5 long.The longest row starts at 90 and stops at 96 and is 7 long.The longest row starts at 114 and stops at 126 and is 13 long.The longest row starts at 524 and stops at 540 and is 17 long.The longest row starts at 888 and stops at 906 and is 19 long.The longest row starts at 1130 and stops at 1150 and is 21 long.The longest row starts at 1328 and stops at 1360 and is 33 long.The longest row starts at 9552 and stops at 9586 and is 35 long.我只需要它來打印最后一張
查看完整描述

2 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

您需要將a和的值保存b到單獨的變量中,以便可以在循環后打印它們。


b = priemen[1]

maxrow = 0


for n in priemen[2:]:

    a = b

    b = n

    row = b - a - 1       

    

    if row > maxrow:

        maxrow = row

        a_max = a

        b_max = b


if maxrow != 0:

    print("The longest row starts at", a_max + 1, "and stops at", b_max - 1, "and is", maxrow, "long.")

其他注意事項:

  • a_max我還沒有初始化b_max- 但最終的if測試是為了防止任何尚未設置的情況

  • 我已重命名nextn,因為next這是內置名稱

  • a = priemen[0]行毫無意義,所以我已將其刪除



查看完整回答
反對 回復 2023-08-03
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

我發現這段代碼效率低下并且存在問題。首先,它的效率很低,因為它測試從 1 到x


for deler in range(1, x):

當它只需要測試從 3 到的平方根的奇數除數(處理偶數之后)時。但即使這樣也是低效的,因為它正在創建一個素數列表,它可以用作除數來進一步加快速度!最后,就效率而言,我相信它可以一次性完成:x


TARGET = 10_000  # below this number


primes = [2]


start = end = primes[-1]


for number in range(3, TARGET, 2):


    def is_prime(number):

        for divisor in primes:

            if divisor * divisor > number:

                return True


            if number % divisor == 0:

                return False


        return True


    if is_prime(number):

        primes.append(number)


        if primes[-1] - primes[-2] > end - start:

            start, end = primes[-2:]


print("The longest run starts at", start + 1, "and stops at", end - 1, "and is", end - start - 1, "long.\n")

最后,就目標而言,問題未明確說明,并且解決方案可能是錯誤的??紤]目標為 9586 而不是 10000。編寫的代碼將打?。?/p>


The longest run starts at 1328 and stops at 1360 and is 33 long.

但是通過在主循環之后添加以下代碼:


if TARGET - primes[-1] > end - start:

    start, end = primes[-1], TARGET

我們得到正確答案:


The longest run starts at 9552 and stops at 9585 and is 34 long.

如果目標更大,跑步會更長,但這仍然是最長的跑步。


查看完整回答
反對 回復 2023-08-03
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號