#34555: Python a010 因數分解 NA求開示


aa03003747@gmail.com (曹涵雅(雅翔))

學校 : 不指定學校
編號 : 229007
來源 : [118.167.219.105]
最後登入時間 :
2023-03-29 23:16:16
a010. 因數分解 | From: [118.167.219.105] | 發表日期 : 2023-03-29 23:50

各位大大好,我想請問我的程式哪邊有疏忽,我用範例輸入三個測資測試都是對的,但是送出解答卻是NA,想破頭了再麻煩各位開示,感謝各位!

n是我的輸入值,篩選出來的因數存放在list1,最後再print出來。

 

n = int(input())
if n <= 100000000 and n > 1:
    list1 = []

    for i in range(2, int(n+1)):
        while n % i == 0:
            list1.append(i)
            n = n // i    
        if list1.count(i) != 0:
            print(i,end = '')
            if list1.count(i) > 1:
                print("^",list1.count(i),sep ='',end = '')
            if n * i != list1[-1]:
                print(" * ",sep = '', end = '') 

 
#34556: Re: Python a010 因數分解 NA求開示


linlincaleb@gmail.com (臨末之頌)

學校 : 新北市立板橋高級中學
編號 : 132772
來源 : [111.248.111.135]
最後登入時間 :
2023-04-01 22:41:13
a010. 因數分解 | From: [111.248.132.23] | 發表日期 : 2023-03-30 09:53

各位大大好,我想請問我的程式哪邊有疏忽,我用範例輸入三個測資測試都是對的,但是送出解答卻是NA,想破頭了再麻煩各位開示,感謝各位!

n是我的輸入值,篩選出來的因數存放在list1,最後再print出來。

 

n = int(input())
if n <= 100000000 and n > 1:
    list1 = []

    for i in range(2, int(n+1)):
        while n % i == 0:
            list1.append(i)
            n = n // i    
        if list1.count(i) != 0:
            print(i,end = '')
            if list1.count(i) > 1:
                print("^",list1.count(i),sep ='',end = '')
            if n * i != list1[-1]:
                print(" * ",sep = '', end = '') 

我猜會TLE吧 這個要跑的時間過長,建議可以讓迴圈跑到sqrt(N)就好 如果N!=1,就代表最後的N是質數。

 

 
ZeroJudge Forum