Python 我的代码没有';我每次运行它都不能工作

Python 我的代码没有';我每次运行它都不能工作,python,Python,给定一个非负整数序列a0,…,一个−1,求最大两两乘积,即通过将序列中的两个不同元素相乘(或者更正式地说,max0)得到的最大整数≤我≠J≤N−1aiaj)。这里的不同元素表示ai和aj,带有i≠j(可能是ai=aj) 输入格式 输入的第一行包含一个整数n。下一行包含n个非负整数a0,…,一个−1 约束条件 2≤N≤2.⋅105; 0≤a0,…,an−1.≤105 输出格式 输出单个数字-最大成对乘积 此代码工作正常,但有时运行时会显示: Traceback (most recent call

给定一个非负整数序列
a0,…,一个−1,
求最大两两乘积,即通过将序列中的两个不同元素相乘(或者更正式地说,
max0)得到的最大整数≤我≠J≤N−1aiaj
)。这里的不同元素表示ai和aj,带有
i≠j
(可能是
ai=aj

输入格式

输入的第一行包含一个整数n。下一行包含n个非负整数
a0,…,一个−1

约束条件

2≤N≤2.⋅105; 0≤a0,…,an−1.≤105

输出格式

输出单个数字-最大成对乘积

此代码工作正常,但有时运行时会显示:

Traceback (most recent call last):
  File "C:\Users\gauta\AppData\Local\Programs\Python\Python35\gen.py", line 26, in <module>
    print(max(c))
ValueError: max() arg is an empty sequence
回溯(最近一次呼叫最后一次):
文件“C:\Users\gauta\AppData\Local\Programs\Python\Python35\gen.py”,第26行,在
打印(最大值(c))
ValueError:max()arg是一个空序列
仅当列表“a”中的元素总数为2或3时,它才会显示这一点

如何改进此代码并修复该问题,以及此代码是否会显示超出时间限制或整数溢出错误

import random  
import time  
b=time.time()  
a=list()  
c=list()   
n=random.randint(2,12)  
#appending random numbers in a list 'a'
g=1  
while(g<=n): 
   a.append(random.randint(0,10))  
   g=g+1  
print(a)  
print("Total elements in the list= %s"%len(a))  
#Appending Done
for i in range(2,n):
    for j in range (2,n):               
       if a[i]*a[j]>0:
           if a[i]!=a[j]:  
               m=a[i]*a[j]  
               c.append(m)   
           else:  
               continue  
      else:  
           continue  
print(max(c))  
time=time.time()-b  
print("%s"%(time.time()-b))  
随机导入
导入时间
b=时间。时间()
a=列表()
c=列表()
n=random.randint(2,12)
#在列表“a”中添加随机数
g=1
而(g0:
如果a[i]!=a[j]:
m=a[i]*a[j]
c、 追加(m)
其他:
继续
其他:
继续
打印(最大值(c))
time=time.time()-b
打印(“%s”%(time.time()-b))

不建议使用变量名,变量名也是您正在使用的模块的名称

import random  
import time  
b=time.time()  
a=list()  
c=list()   
n=random.randint(2,12)  
#appending random numbers in a list 'a'
g=1  
while(g<=n):
    a.append(random.randint(0,10))
    g=g+1  
print(a)  
print("Total elements in the list= %s"%len(a))  
#Appending Done
for i in range(2,n):
    for j in range (2,n):
        if a[i]*a[j]>0:
            if a[i]!=a[j]:  
                m=a[i]*a[j]  
                c.append(m)   
            else:  
                continue  
        else:  
            continue  
print(max(c))  
othertime=time.time()-b
print(type(othertime)) #here in this line you changed the time to float     value hence now onwards you can't use time module as you were using before hence i renamed time variable to other.   
print("%s" %(time.time()-b))  
随机导入
导入时间
b=时间。时间()
a=列表()
c=列表()
n=random.randint(2,12)
#在列表“a”中添加随机数
g=1
而(g0:
如果a[i]!=a[j]:
m=a[i]*a[j]
c、 追加(m)
其他:
继续
其他:
继续
打印(最大值(c))
othertime=time.time()-b
打印(键入(othertime))#在这一行中,您将时间更改为浮点值,因此从现在开始,您不能像以前那样使用时间模块,因此我将时间变量重命名为其他。
打印(“%s”%(time.time()-b))

通过将变量名time更改为other time,您可以再次使用time module,因此请记住,以后不要使用其他模块名或关键字name来命名变量,否则它们的行为将在代码中丢失。

不建议使用变量名,变量名也是您正在使用的模块名

import random  
import time  
b=time.time()  
a=list()  
c=list()   
n=random.randint(2,12)  
#appending random numbers in a list 'a'
g=1  
while(g<=n):
    a.append(random.randint(0,10))
    g=g+1  
print(a)  
print("Total elements in the list= %s"%len(a))  
#Appending Done
for i in range(2,n):
    for j in range (2,n):
        if a[i]*a[j]>0:
            if a[i]!=a[j]:  
                m=a[i]*a[j]  
                c.append(m)   
            else:  
                continue  
        else:  
            continue  
print(max(c))  
othertime=time.time()-b
print(type(othertime)) #here in this line you changed the time to float     value hence now onwards you can't use time module as you were using before hence i renamed time variable to other.   
print("%s" %(time.time()-b))  
随机导入
导入时间
b=时间。时间()
a=列表()
c=列表()
n=random.randint(2,12)
#在列表“a”中添加随机数
g=1
而(g0:
如果a[i]!=a[j]:
m=a[i]*a[j]
c、 追加(m)
其他:
继续
其他:
继续
打印(最大值(c))
othertime=time.time()-b
打印(键入(othertime))#在这一行中,您将时间更改为浮点值,因此从现在开始,您不能像以前那样使用时间模块,因此我将时间变量重命名为其他。
打印(“%s”%(time.time()-b))

通过将变量名time更改为other time,您可以再次使用time模块,因此请记住,以后不要使用其他模块名或关键字name命名变量,否则它们的行为将丢失在您的代码中。

您的代码有两个缺陷:

  • 您使用
    range(2,n)
    运行两个循环,但是
    n
    可以随机设置为
    2
    range(2,2)
    是一个空序列,因此您的循环体将不会运行,您将得到一个空列表
    c

  • 通过将
    time.time()-b
    表达式的结果赋给名称
    time
    来屏蔽该名称。任何进一步尝试访问
    time.time
    的操作都将为您提供一个
    AttributeError
    ,因为浮点对象没有此类属性。请重命名该变量

接下来,您将使用O(N^2)方法;
a
中元素数量每增加一次,时间就会呈指数增长。这肯定会很快达到时间限制。您只需找到
a
中的两个最大整数并将其相乘;这可以在O(N)线性时间内完成。因此,如果
len(a)
为1000,您的方法需要100万步,而线性时间方法只需要1000步

查找序列中K个最大数的最有效方法是使用,它在O(NlogK)时间内查找这些数;对于固定的K=2,这使其成为O(N)线性时间方法。您可以使用将找到的两个整数相乘:

import heapq
import operator

if len(a) > 1:
    result = operator.mul(*heapq.nlargest(2, a))
elif a:
    result = a[0]  # only one number, it's the largest
else:
    result = None  # no numbers, no result

您的代码有两个缺陷:

  • 您使用
    range(2,n)
    运行两个循环,但是
    n
    可以随机设置为
    2
    range(2,2)
    是一个空序列,因此您的循环体将不会运行,您将得到一个空列表
    c

  • 通过将
    time.time()-b
    表达式的结果赋给名称
    time
    来屏蔽该名称。任何进一步尝试访问
    time.time
    的操作都将为您提供一个
    AttributeError
    ,因为浮点对象没有此类属性。请重命名该变量

接下来,您将使用O(N^2)方法;
a
中元素数量每增加一次,时间就会呈指数增长。这肯定会很快达到时间限制。您只需找到
a
中的两个最大整数并将其相乘;这可以在O(N)线性时间内完成。因此,如果
len(a)
为1000,您的方法需要100万步,而线性时间方法只需要1000步

查找序列中的K个最大数的最有效方法是使用,它在O(NlogK)时间内查找这些数;对于固定的K=2,这使其成为O(