繁体   English   中英

Python,找到数字的第n个根

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM