三个数字的乘积,如果有,则打印 NO (python)
Product of three numbers, if there is any, else print NO (python)
我在 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
我试过了,除了最后一个示例输出外,它都有效。我不确定出了什么问题以及如何解决这个问题。任何帮助,将不胜感激。 (我知道使用 'or' 会更好,但我想确保包含所有条件以更好地了解问题所在。)
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
我的尝试:
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,int(i*0.5)): #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]
我在 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
我试过了,除了最后一个示例输出外,它都有效。我不确定出了什么问题以及如何解决这个问题。任何帮助,将不胜感激。 (我知道使用 'or' 会更好,但我想确保包含所有条件以更好地了解问题所在。)
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
我的尝试:
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,int(i*0.5)): #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]