[英]Python, Finding nth root of a number
我必須找到n的m根。 t表示測試用例的數量,對於每個測試用例,n和m的行之間用空格隔開。輸出格式:對於每個測試用例,如果它是整數n的打印m根,否則打印-1。
我正確完成的每個變量也有一些約束。 像1<=t<=10^5 1<n<30 1<=m<=10^9
。 我不知道這是什么錯誤。
t= int (input())
if (t>=1) and (t<=100000) :
while (t>0) :
no=input()
e=no.split()
n=int (e[0])
m=int (e[1])
if(n>1) and (n<30) and (m>=1) and (m<=1000000000):
k=m**(1/n)
if (k==int(k)) :
print (int(k))
else :
print(-1)
else :
print(-1)
t=t-1
else:
print(-1)
以下代碼有效:
import math
t= int (input())
if (t>=1) and (t<=100000) :
while (t>0) :
no=input()
e=no.split()
n=int (e[0])
m=int (e[1])
if(n>1) and (n<30) and (m>=1) and (m<=1000000000):
k=m**(1/n)
#print(math.ceil(k*100)/100)
l=int (math.ceil(k*100)/100)
#print(l**n)
r=l**n
if (r==m) :
print (l)
else :
print(-1)
else :
print(-1)
t=t-1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.