Python用于最大配对产品

时间:2016-10-10 02:33:02

标签: python product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()3

for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue
print(max(c))

此代码有效。但是,我想定义一个函数来自动计算下面代码中第5行的最大乘积。

def MaxPairwiseProduct(n,a,c):
for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue

        Product = max(c)

        return Product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()
MaxPairwiseProduct(n,a,c)

我重写了这个功能,但它不起作用。它揭示了" IndentationError:期望一个缩进的块"

5 个答案:

答案 0 :(得分:0)

struct product {
   int code;
   string description;
   double price;
   int stock;
   product* next;
};

答案 1 :(得分:0)

# Uses python3
def MaxPairwiseProduct(n,a,c):
    for i in range(0,n):
        for j in range (1,n):
            if a[i] != a[j]:
                m = a[i]*a[j]
                c.append(m)

            else:
                continue

    Product1 = max(c)
    return Product1

def MaxPairwiseProductFast(n,a):
    max_index1 = -1
    for i in range(0,n):
        if a[i] > a[max_index1]:
            max_index1 = i
        else:
            continue

    max_index2 = -1
    for j in range(0,n):
        if a[j] > a[max_index2] and a[j] != a[max_index1]:
            max_index2 = j   
        else:
            continue

    Product2 = a[max_index1]*a[max_index2]
    return Product2

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()

print('The max value by regular algorithm:', MaxPairwiseProduct(n,a,c))
print('The max value by faster algorithm:', MaxPairwiseProductFast(n,a))  

此代码包含计算最大值的第二种算法。

答案 2 :(得分:0)

我对最大配对产品的愚蠢解决方案。

   n = int(input()) #
   nums = input().strip().split()
   nums = [ int(i) for i in nums ]
   firstmax, secondmax = sorted(set(nums))[-2:]
   print(firstmax*secondmax)

答案 3 :(得分:0)

# python3
result = 0
def max_pairwise_product_fast(n, numbers):
    max_index1 = -1
    for i in range(n):
        if max_index1 == -1 or numbers[i] > numbers[max_index1]:
            max_index1 = i
    max_index2 = -1

   for i in range(n):
        if i != max_index1 and (max_index2 == -1 or numbers[i] > numbers[max_index2]):
            max_index2 = i

    return numbers[max_index1] * numbers[max_index2]
if __name__ == '__main__':
    n = int(input())
    a = [int(x) for x in input().split()]
    assert (len(a) == n)

    print(max_pairwise_product_fast(n, a))

答案 4 :(得分:0)

进行压力测试:您可以在此处使用此代码。当我做作业时,它会起作用。

#压力测试

来自随机进口randint

def max_prod_stress(N,M):

while True:
    
    n = randint(2,N)
    
    A = [None]*n
    
    for i in range(n):
        
        A[i] = randint(0,M)
    
     print(A)
    
     result1 = max_prod_naive(A)
    
     result2 = max_prod_fast(A)
    
      if result1==result2:
        
      print('OK')
    
      else:
        
          print('Wrong answer:',result1,result2)
        
      return

           max_prod_stress(5,100)