简体   繁体   English

提取两个元素的数量,它们的总和等于它们的乘积

[英]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)

example:例子:

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.

相关问题 我写了一个代码,应该识别序列的哪些元素等于两个不同的arrays的元素之和,但它是错误的 - I wrote a code that should identify which of the elements of the sequence are equal to the sum of the elements of two different arrays, but it's wrong 查找数组中两个数字的总和是否等于 k - Find if sum of two numbers in an array is equal to k Python总和两个数字之间的元素 - Python sum elements between two numbers 确保两个随机生成的数字之和等于另外两个随机生成的数字之和 - Ensure that the sum of two randomly generated numbers is equal to the sum of another two randomly generated numbers 从两个列表中找到2个数字的最快方法,总和等于x - The fastest way to find 2 numbers from two lists that in sum equal to x 两个排序数组,两个元素之和等于一定数目 - Two Sorted Arrays, sum of 2 elements equal a certain number 用张量流乘以两个小数 - Multiplication of two small numbers with tensorflow 如何找到两个总和等于Python中目标总和的索引? - How can I find the indices of any two of the numbers, whose sum is equal to the target sum in Python? 返回两个 numpy 数组中的哪些元素都等于 X 值? - Return which elements in two numpy arrays are both equal to X value? 两个列表中所有元素的相乘 - Multiplication of all elements in two lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM