[問題] hackerrank - Maximize It

看板Python作者 (沒有真心就別談感情)時間2年前 (2021/09/23 23:23), 2年前編輯推噓2(202)
留言4則, 2人參與, 2年前最新討論串1/1
https://www.hackerrank.com/challenges/maximize-it/problem 第一行輸入:數列數 某整數n 第二行之後為依序輸入數列內容 每個數列取一個元素,再把每個元素平方和之後去餘數n=Smax (mod n或%n) 找到Smax為多少就是答案,就是六個數列各取一元素平方和再餘數n的最大值! Sample Input 3 1000 2 5 4 3 7 8 9 5 5 7 8 9 10 Sample Output 206 我的解答如下: from itertools import product a=input().split() pr=[] for i in range(0,int(a[0])): b=input().split() pr.append(b) prsum = product(*pr) maxi=0 for items in prsum: subsum = 0 for item2s in items: if int(item2s) >int(a[1]): subsum = subsum + (int(item2s)%int(a[1]))*(int(item2s)%int(a[1])) else: subsum = subsum + int(item2s)*int(item2s) subsum = subsum % int(a[1]) if subsum > maxi: maxi = subsum print(maxi) 17個測試過了13個,四個失敗 我解鎖了一個測試內容如下: Input (stdin) 6 767 2 488512261 423332742 2 625040505 443232774 1 4553600 4 92134264 617699202 124100179 337650738 2 778493847 932097163 5 489894997 496724555 693361712 935903331 518538304 Expected Output 763 但我的程式碼給的是766 我找到一組 組合 488512261 2 1 337650738 2 518538304 這六個數字平方和之後餘數767明明就是766 不曉得有沒有人可以解我的惑 ~'~ 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.116.228.113 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1632410634.A.5A3.html ※ 編輯: newbrain (122.116.228.113 臺灣), 09/23/2021 23:26:44 ※ 編輯: newbrain (122.116.228.113 臺灣), 09/23/2021 23:27:18

09/24 00:30, 2年前 , 1F
你的答案看起來沒錯啊
09/24 00:30, 1F

09/24 01:54, 2年前 , 2F
看了一下題目 應該是你input data處理有誤 3~8行的開頭
09/24 01:54, 2F

09/24 01:55, 2年前 , 3F
是該行的元素個數 處理時要剔除
09/24 01:55, 3F

09/24 07:52, 2年前 , 4F
太感謝大大了!
09/24 07:52, 4F
文章代碼(AID): #1XJ9mAMZ (Python)