我得到了以下作業來用python解決。
給你一個整數 n。找到三個不同的整數 a,b,c 使得 2≤a,b,c 和 a?b?c=n 或者說不可能做到。
如果有多個答案,您可以列印任何一個。
你必須回答 t 個獨立的測驗用例。輸入的第一行包含一個整數 t (1≤t≤100) — 測驗用例的數量。
接下來的 n 行描述了測驗用例。第 i 個測驗用例作為一個整數 n 在新行上給出。
輸入:5=t, 64, 32, 97, 2, 12345
輸出:是,2 4 8,否,否,否,是,3 5 823
我已經嘗試過了,除了最后一個示例輸出之外它都可以作業。我不確定出了什么問題以及如何解決這個問題。任何幫助,將不勝感激。(我知道使用“或”會更好,但我想確保我包含所有條件以更好地查看錯誤所在。)
import math
def divisors(t,n):
for k in range(t,0,-1):
a=0
b=0
c=0
k=0
l=n
for i in (2,math.sqrt(n)):
if n%i==0 :
if k==0:
a=i
n =n/a
elif k==1:
b=i
k=k 1
if k==2:
break
if (a==1 or b==1):
print('NO')
break
if a*b==0:
print('NO')
break
if a==b:
print('NO')
break
c=l/(a*b)
if c==b:
print('NO')
break
if a==c:
print('NO')
break
if(c<2):
print('NO')
break
else:
print('YES', int(a ),int(b ), int(c))
break
uj5u.com熱心網友回復:
我的嘗試:
import numpy as np
def div(cases): #Python can work without indexing (No of cases).
for i in cases:
r = [] #Here, I store all the factors. Is reset every case.
for j in range(2,i 1): #Loop through all the possible divisors.
if i % j == 0: #Check if the case has no remainder with the current factor.
r = [j] #If factor found, add to list
i = i//j #Divide the case by the found factor
if len(r) < 3:
print('NO')
else:
print(f'YES: {[r[0], r[1], np.prod(r[2:])]}') #Print 3 and 3 numbers only
#print(f'YES: {r}')
輸入:
div([64, 32, 97, 2, 12345])
輸出:
YES: [2, 4, 8]
NO
NO
NO
YES: [3, 5, 823]
在這里運行它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442327.html
上一篇:如何在執行BFS/DFS演算法時從遍歷路徑中找到最終路徑
下一篇:將Scratch轉換為演算法
