#22574: 請問為什麼測資3一直TLE?


ironman30832@gmail.com (Wang YuChun)

學校 : 國立臺北科技大學
編號 : 105311
來源 : [61.220.20.52]
最後登入時間 :
2023-10-24 09:33:02
a010. 因數分解 | From: [140.124.135.12] | 發表日期 : 2020-09-17 02:48

import math
try: 
    while 1:
      
        n = int(input())
        box2=0
        a = ""
        b = ""
        c = 0
        d=n
  
        for i in range(2,n-1):
            
            if i == int(d/2):
                break
            while n%i==0:
               
                n=n/i
                box1 = i
                box2 += 1

                if box2 ==1:
                    if c>=1:
                        b = " * " +str(box1)
                    else:
                        b = str(box1)
                else:
                    if c>=1:
                        b = " * "+ str(box1)+"^"+str(box2)
                    else:
                        b = str(box1)+"^"+str(box2)
            if b !="":
                c+=1
            a+=str(b)
            b = ""
            box2 = 0
        if a == "":
            print(n)
        else:
            print(a)
 
except EOFError:       
    pass
 
#22575: Re:請問為什麼測資3一直TLE?


ironman30832@gmail.com (Wang YuChun)

學校 : 國立臺北科技大學
編號 : 105311
來源 : [61.220.20.52]
最後登入時間 :
2023-10-24 09:33:02
a010. 因數分解 | From: [140.124.135.12] | 發表日期 : 2020-09-17 02:58

import math
try: 
    while 1:
      
        n = int(input())
        box2=0
        a = ""
        b = ""
        c = 0

        
        
        for i in range(2,n-1):
            
            if n <2:
                break
            while n%i==0:
               
                n=n/i
                box1 = i
                box2 += 1

                if box2 ==1:
                    if c>=1:
                        b = " * " +str(box1)
                    else:
                        b = str(box1)
                else:
                    if c>=1:
                        b = " * "+ str(box1)+"^"+str(box2)
                    else:
                        b = str(box1)+"^"+str(box2)
            if b !="":
                c+=1
            a+=str(b)
            b = ""
            box2 = 0
        if a == "":
            print(n)
        else:
            print(a)
       
        
          
except EOFError:       
    pass
 
抱歉 剛發現把break判斷改成 if n <2 就可以了
 
ZeroJudge Forum