思路是统计每行中的每个元素与最后一个元素相减,减的值存到新数组,用tuple做哈希,遍历哈希,如果一个tuple的相反tuple在里面,就加对应的两个value相乘(tuple的value与-tuple的value),最后结果除2。用例过了,可提交就变0
实在不知道哪里错了 求大佬们帮忙看看
def result(a): n=len(a) k=len(a[0]) b=[[0 for i in range(k-1)]for j in range(n)] for i in range(n): for j in range(k-1): b[i][j]=a[i][j]-a[i][k-1] d=[] for i in b: d.append(tuple(i)) s={} for i in d: if i in s:s[i]+=1 else:s[i]=1 count=0 for i in s: if reverse(i) in s: count+=s[reverse(i)]*s[i] return count // 2 def reverse(t:tuple)->tuple:#返回tuple的相反数 l=list(t) for i in range(len(l)): l[i]=-l[i] return tuple(l) str1 = input() strs = str1.split() n, m = int(strs[0]), int(strs[1]) a = [] for _ in range(n): str_tmp = [int(i) for i in input().split()] a.append(str_tmp) print(result(a))
全部评论
(4) 回帖