为什么python可以正确求和?
我有这个代码片段:
import math
number_of_lists, M = list(map(int, input().split(" ")))
print(sum([math.pow(max(list(map(int, input().split(" ")))), 2) for i in range(number_of_lists)]) % M)
我要处理的数字很大。当我计算最终结果时,它与Google Calculator并不相同。有人知道为什么是吗?当我用Google计算器的最终值替换总值时,它会返回正确的答案。我尝试将其更改为Float,但它不起作用,我环顾四周,寻找某种较大的数据类型,但似乎INT应该能够处理这些数字。
这是一个示例输入:
7 671
7 5678403 6770488 5713245 6503478 7774748 5900452 531896
7 7728332 501199 9141815 7341382 7238970 8282671 3037527
7 7763981 7041667 3521352 9616160 7322888 5685405 6017382
7 7278231 1143649 6460915 8159948 2436146 1238439 9869216
7 1422820 9424407 4982886 7101222 8711246 696130 6121051
7 6485993 6596581 9169298 4214325 7097779 827465 4072058
7 6853100 9110135 9625936 7133432 8668153 5663640 6749591
它应该返回670,但返回53。 抱歉,如果代码有点局促,我只是想用最少的行求解。 这是练习的链接: https> https://wwwww.hackerrank。 com/挑战/最大化/问题?isfullscreen = true
I have this code snippet:
import math
number_of_lists, M = list(map(int, input().split(" ")))
print(sum([math.pow(max(list(map(int, input().split(" ")))), 2) for i in range(number_of_lists)]) % M)
Im dealing with really LARGE numbers. When I calculate the final result it doesn't yeild the same as the google calculator. Does anyone know why is that. When I replace the final value of the sum with that of the google calculator it returns correct answer. I tried changing it to float but it didn't work, I looked around for some kind of bigger datatype, but it seems as int should be able to handle the numbers.
Here is a sample input:
7 671
7 5678403 6770488 5713245 6503478 7774748 5900452 531896
7 7728332 501199 9141815 7341382 7238970 8282671 3037527
7 7763981 7041667 3521352 9616160 7322888 5685405 6017382
7 7278231 1143649 6460915 8159948 2436146 1238439 9869216
7 1422820 9424407 4982886 7101222 8711246 696130 6121051
7 6485993 6596581 9169298 4214325 7097779 827465 4072058
7 6853100 9110135 9625936 7133432 8668153 5663640 6749591
it should return 670, but it return 53.
Sorry if the code is a bit cramped up I'm just trying to solve it with the least amount of lines.
Here is a link to the exercise: https://www.hackerrank.com/challenges/maximize-it/problem?isFullScreen=true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此代码以查看是否可以提供帮助 - 它简化了读取和
处理
,并使其更可读
(用于维护和调试)。有时,我们想打动人们并制作单线或紧凑的代码,从而牺牲可读性。我不建议这样做。
Try this code to see if it can help - it simplify the reading and
processing
and make it morereadable
(for maintain and debug).Sometimes, we want to impress people and make one-liner or compact code, thus sacrifice the readability. I don't recommend that.