给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。

编程入门 行业动态 更新时间:2024-10-25 06:22:11
本文介绍了给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

现在这个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.

更多推荐

给定一组整数,找到具有最大乘积的相邻元素对并返回该乘积。

本文发布于:2023-11-30 12:15:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:乘积   整数   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!