为什么python可以正确求和?

发布于 2025-02-11 01:16:19 字数 1092 浏览 2 评论 0原文

我有这个代码片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2025-02-18 01:16:19

尝试此代码以查看是否可以提供帮助 - 它简化了读取和处理,并使其更可读(用于维护和调试)。

有时,我们想打动人们并制作单线或紧凑的代码,从而牺牲可读性。我不建议这样做。

import itertools as it

K, M = map(int, input(), split())   

# reading the K lines and appending lists to LL
LL = []

for i in range(K):
    ll = list(map(int, input().strip().split()))
    LL.append(ll[1:])    # skip the first num

MAX = -1

# looping thr Cartesian Product of LL lists and get the max now
for i in it.product(*LL):
    MAX = max(sum(map(lambda x: x**2, i)) % M, MAX)

print(MAX)     

Try this code to see if it can help - it simplify the reading and processing and make it more readable (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.

import itertools as it

K, M = map(int, input(), split())   

# reading the K lines and appending lists to LL
LL = []

for i in range(K):
    ll = list(map(int, input().strip().split()))
    LL.append(ll[1:])    # skip the first num

MAX = -1

# looping thr Cartesian Product of LL lists and get the max now
for i in it.product(*LL):
    MAX = max(sum(map(lambda x: x**2, i)) % M, MAX)

print(MAX)     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文