2016-10-10 3 views
0
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 번째 줄부터 최대 제품을 자동으로 계산하는 함수를 정의하고 싶습니다.최대 pairwise 제품을위한 파이썬

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"

+0

'else : continue'는 불필요합니다. –

+0

중복 된 http://stackoverflow.com/questions/39329829/how-to-find-the-maximum-product-of-two-elements-in-a-list? – kennytm

+0

@ juanpa.arrivillaga : 아니에요. 계산을 우회하고'Product'를 반환합니다. 물론 더 나은 방법이 있지만 코드를 제거하면 코드의 동작이 변경됩니다. – zondo

답변

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
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() 
print(MaxPairwiseProduct(n,a,c)) 
+0

이것은 초보자에게 좋은 연습입니다. –

0

최대 pairwise 제품에 내 바보 솔루션.

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