给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。 [英] Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

查看:145
本文介绍了给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这个lis列表中我怎么能找到一个最大数字???

注意:max()显示一个NoneType错误



我尝试过的事情:



Now out of this list of lis how can I find a maximum number???
NOTE: max() is showing a NoneType Error

What I have tried:

def adjacentElementsProduct(inputArray):
    for i in range(0, len(inputArray)-1):
        lis=(inputArray[i]*inputArray[i+1])
        print(lis)

推荐答案

您要查找的是列表中两个数字的最大乘积:这意味着将每个数字相乘并将每个数字与之前的最大值进行比较,而不仅仅是打印每个产品。 />
所以试试这个:

1)在循环外创建一个变量,称之为 maxSoFar 并将其设置为第一对。

2)遍历每个元素(最后一个元素除外)

2.1)将每个元素乘以后面的元素。

2.2)比较该值 maxSoFar

2.2.1)如果它更大,请将 maxSoFar 设置为新产品。

3)继续循环

4)循环后,打印 maxSoFar



简单!但是......这是你的功课,所以我不会给你任何代码!试一试 - 这真的不是一项艰巨的任务。
What you are looking for is the largest product of two numbers in a list: which means multiplying each pair and comparing each against the previous maximum, not just printing each product.
So try this:
1) Create a variable outside the loop, call it maxSoFar and set it to the product of the first pair.
2) Loop through each element (except the final one)
2.1) Multiply each element by the one after it.
2.2) Compare that value with maxSoFar
2.2.1) If it's larger, set maxSoFar to the new product.
3) Continue with the loop
4) After the loop, print maxSoFar

Simple! But ... this is your homework, so I'll give you no code! Give it a try - this really isn't a difficult task.


这篇关于给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆