“美丽数组”CodeChef,如何输入d

2024-06-17 11:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

问题陈述如下:

An array a is called beautiful if for every pair of numbers ai, aj, (i ≠ j), there exists an ak such that ak = ai * aj. Note that k can be equal to i or j too.

Find out whether the given array a is beautiful or not!

Input

First line of the input contains an integer T denoting the number of test cases. T test cases follow.

First line of each test case contains an integer n denoting number of elements in a.

Next line contains n space separated integers denoting the array a. Output

For each test case, output a single line containing "yes" or "no" (without quotes) corresponding to the answer of the problem. Constraints

1 ≤ T ≤ 106
1 ≤ n ≤ 105
Sum of n over all the test cases ≤ 106
-109 ≤ ai ≤ 109

Example

Input

3

2

0 1

2

1 2

2

5 6

Output:

yes

yes

no

我刚接触CodeChef,不知道如何正确地接收输入,我有一些代码,我试图用它来解决美丽的数组问题,但当我运行它时,我得到了一个NZEC。在

from collections import Counter
numCase = int(input())
for i in range(numCase):
    length = input()
    array = Counter(input().split(''))
    answer = "no"
    for i in range(length - 1):
        if array[i] == 0 or array[i] == 1:
            answer = "yes"

    print(answer)

当我更改输入并手动输入一些数据时,代码对我有效,有人能告诉我为什么这个代码不起作用吗?谢谢


Tags: oroftheanswertestanforinput
2条回答

我想你没有检查数组的最后一个元素是因为范围。试试这个

for i in range(length)

首先,当您手动运行代码时,它会崩溃。您至少需要将length = input()更改为length = int(input())。NZEC是一个运行时错误,它是CodeChef,告诉您您的代码正在崩溃。在

其次,最好不要参考外部网站。给你潜在的答案一个战斗的机会,给我们这个问题。查找CodeChef、它是什么以及在哪里可以找到问题语句并不方便。另外,CodeChef可能会消失,或者可能关闭,然后您显然无法得到答案。话虽如此,我已经把问题陈述放在你的问题中了。在

最后,您的解决方案在逻辑上存在根本缺陷(不仅仅是崩溃)。仅检查数组中的前两个项是否分别是0和{}是不够的。不过,你已经接近有一个有效的解决方案了。观察以下输入/输出:

1

3

1 -4 -1

# Output should be "no", since -4 * -1 == 4, and 4 is not in the list.

另外,考虑一下

^{pr2}$

基本情况(如果数组len==1)是“yes”输出。这在我看来是不正确的(我不能等于j,长度为1的输入对没有很好的定义,我可以继续…)。所以我愿意给你这部分的解决方案,因为它也愚弄了我,它不是特别清楚。在

作为旁注,我似乎在这篇文章中看到了对这个问题的一些误解,所以我将尝试添加一些澄清。在

An array a is called beautiful if for every pair of numbers ai, aj, (i ≠ j), there exists an ak such that ak = ai * aj. Note that k can be equal to i or j too.

这里重要的词是“对于每一对数字ai,aj存在ak”,而不仅仅是某些对。这意味着如果我们有输入数组[2, 3, 6],答案是“no”,因为3*6==18,但是18不在数组中。很容易认为答案应该是“是”,因为2*3==6,但这只是一些满足关系的对。这个问题清楚地表明,每个对都必须满足这个关系。在

这是我的解决方案。我很想不贴出来,但如果你真的卡住了,这可以作为你的参考。我强烈建议你不要马上看解决方案,至少给自己一整天的时间来解决它,而且一定要睡一觉。通过将我的解决方案复制/粘贴到CodeChef中,您不会学到任何东西。将鼠标悬停在下面的部分以查看我的解决方案。在

numCase = int(input())
for i in range(numCase):
    length = int(input())
    array = [int(x) for x in input().split(' ')]
    num_neg_ones = len([x for x in array if x == -1])
    num_ones = len([x for x in array if x == 1])
    num_other = len([x for x in array if x not in [0, -1, 1]])

    answer = "yes"
    if num_other > 1:
        answer = "no"
    if num_other == 1 and num_neg_ones > 0:
        answer = "no"
    if num_neg_ones > 1 and num_ones == 0:
        answer = "no"
    print(answer)

相关问题 更多 >