(3.6)(1e18 + 1)(1e18)/(1e18)的除法在Python 3.6中给出(1e18)

Sahithi Borra

下面是代码&对于输入n =(1e18 + 1)*(1e18)和k =(1e18),代码应打印“ YES”。但它显示“否”。这是因为'a'的值为1e18。但理想情况下,“ a”应为(1e18 + 1)。请帮忙。

    for t in range(int(input())):
        n,k=input().split()
        n=int(n)
        k=int(k)
        if n>=k*k:
            a=n/k
            print("a is {}".format(a))
            if a%k==0: 
                print ("a%k is {} ans {}".format(a%k, "NO"))
            else: 
                print("a%k is {} ans {}".format(a%k, "YES"))
        else:
            print("YES")

输入:

3
1 1
30 5
1000000000000000001000000000000000000 1000000000000000000

输出:

a is 1.0
a%k is 0.0 and ans is NO
a is 6.0
a%k is 1.0 and ans is YES
a is 1e+18
a%k is 0.0 and ans is NO
蒂埃里·拉图耶(Thierry Lathuille)

这里的问题是您使用的是“正常”/除法,该除法总是产生浮点数。由于float的精度有限,因此1e18 + 1会四舍五入为1e18。

要解决该问题,请使用//给出整数的整数除法

通过更改此行:

a = n // k

您的代码有效,如以下示例输出所示:

1
1000000000000000001000000000000000000 1000000000000000000
a is 1000000000000000001
a%k is 1 ans YES

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Int除法:为什么1/3 == 0的结果?

使用Pyinstaller创建.exe错误:程序集amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none找不到

1 mod 3的说明

E:找不到'docker-ce-cli'的版本'18 .06.1〜ce〜3-0〜ubuntu'

postgres我得到了一组数字,例如“ 1,2,3,6,7,8,11,12,15,18,19,20”,以获得每组连续数字的最大值

Python 3 print([1、2]和3)输出3

更新选择=(选择%6)+ 1从python 2到python 3

CHKDSK错误代码766f6c756d652e63 3f1

〜1和〜0在python 3中给出奇怪的结果

f = lambda n:(1,f(n-1)* n)[n> 1]在Python 3中给出RunTimeError

递归函数的序列1、3、8、18、38、78

需要Linux Mint 18的Intel Xeon E3-1200驱动程序

(e1 e2 e3)至(m1 m2 m3)仅使用地图和/或应用(方案)

在python 3编程中格式化时无法摆脱1e-5

数据日期8/31/18,Excel图形8/1/18

sed:-e表达式#1,字符3:意外的`,'

3功能1输入

为什么(1 + 3)[a]与a [1 + 3]相同?

如果display(15)我想要结果1,2,3,4,5,6,8,9,10,12,15,16,18,20,24

E: 来自 Depends 的损坏包:python3 (>= 3.6.6-1~)

列族 ID 不匹配(发现 52ac10b0-6e1b-11e7-82d3-c39cc53c1347;预期为 42e51050-6e1b-11e7-82d3-c39cc53c1347)

我希望输出像 [[1,2,3,4,5,6,7,8,9,10],[2,4,6,8,10,12...18,20] ,[3,6,9...27,30].....[9,18,27..90]]

Angular 应用程序 ng build --prod 失败,并在来自 Terser 的 scripts.6e3f538f18f55b02ca8a.js 中出现错误

如何使用 matplotlib 在 Python 中绘制 f(x) = e^x - 4x^3 + 1?

Python 3 - __init__() 需要 1 到 5 个位置参数,但给出了 6 个

Python:find_element() 需要 1 到 3 个位置参数,但给出了 6 个

變量的動態命名(E1、E2、E3 ...)

3表成1

我如何制作 `foo[1] ==1; 富[1][2]==3;foo[1][2][3]==6` 在 JavaScript 中工作?