[英]Extracting the numbers of two elements which their sum is equal to their multiplication
we have array A that has N len(N = the len of the array) I have to found the numbers of pairs (J, I) that work on the following statement:我们有一个数组 A,它有 N len(N = 数组的 len) 我必须找到对 (J, I) 的数量,它们适用于以下语句:
A[J]+A[I} == A[J] * A[I]
1<=i, j<=N 1<=我,j<=N
(1 ≤ N ≤ 40000) (1≤N≤40000)
(0 ≤ A(i) ≤ 10^9) (0 ≤ A(i) ≤ 10^9)
input:输入:
3
2 4 2
output:输出:
1
well i counldn't know how to limit the input size to only have 2 spaces or to split it if there was any more than the N好吧,我不知道如何将输入大小限制为只有 2 个空格,或者如果超过 N 个空格则将其拆分
edit*编辑*
A = []
N = int(input(""))
B = (input(""))
B = B.split()
z = 0
myList = []
mylist2= []
pairs = 0
for q in B:
if z < N:
myList.append(q)
z += 1
elif z >= N:
break
for w in myList:
w = int(w)
mylist2.append(w)
for i in mylist2:
for k in mylist2:`enter code here`
if i + k == i * k:
pairs+1
that what i have done so far那我到目前为止所做的
So, as already mentioned in comments only pairs (2, 2)
and (0, 0)
satisfy the condition.因此,正如评论中已经提到的,只有
(2, 2)
和(0, 0)
对满足条件。 The number of (0, 0)
pairs is count(0) * (count(0) - 1) / 2
. (0, 0)
对的数量是count(0) * (count(0) - 1) / 2
。 The same for (2, 2)
pairs. (2, 2)
对也一样。 Expressing this in python (assuming that array a
is given).在 python 中表达它(假设给出了数组
a
)。
def countsumprod(a):
c0 = a.count(0)
c2 = a.count(2)
return (c0 * (c0 - 1) + c2 * (c2 - 1)) // 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.