zoukankan      html  css  js  c++  java
  • 无序数组中三个数字乘积最大值

    nums = [3472,-7098,-9281,7789,7955,6101,5051,7778,3090,7423,-7151,5652,1595,-8094,677,-8324,8347,-2482,9313,-9338,-3157,8559,6945,3618,3087,121,-8468,3225,1356,6939,2799,-7231,-6309,-5453,633,-8689,-4776,2714,-2743,-1409,5918,-3333,1803,8330,-2206,-6117,-4486,-7903,-4375,-3739,2897,8056,-5864,-522,7451,-4541,-2813,5790,-532,-6517,925]
    res = nums[:3]
    res.sort()
    res_max ,res_min = res[::-1],res[:2]
    
    i = 3
    while i <len(nums):
        if nums[i]>res_max[0]:
            res_max[2],res_max[1],res_max[0] = res_max[1],res_max[0],nums[i]
        elif nums[i]>res_max[1]:
            res_max[2],res_max[1] = res_max[1],nums[i]
        elif nums[i]>res_max[2]:
            res_max[2] = nums[i]
        if nums[i]<res_min[0]:
            res_min[1] = res_min[0]
            res_min[0] = nums[i]
        elif nums[i]<res_min[1]:
            res_min[1] = nums[i]
        i+=1
    
    print (max(res_max[0]*res_max[1]*res_max[2],res_max[0]*res_min[0]*res_min[1]))
    

      注:

    数组中有正数、负数、零。用两个列表,一个存储最大的Top3元素,一个存储最小的Top2元素。乘积最大的情况有两种:一个是三个最大的整数乘积,一个是最大的正数和最小两个负数的乘积。

  • 相关阅读:
    Exection throws和throw
    Exection(二)
    JAVA-Exception
    IDEA初见---输出HelloWorld,并打包成jar包
    Centos6.5安装Tomcat
    Centos6.5下装ZooKeeper
    LInux下装jdk
    python作业高级FTP
    tcp黏包
    计算器
  • 原文地址:https://www.cnblogs.com/kingshine007/p/11396577.html
Copyright © 2011-2022 走看看