题解 | #两个负数+三个正数->最大乘积#

最大乘积

http://www.nowcoder.com/practice/5f29c72b1ae14d92b9c3fa03a037ac5f

#include <bits/stdc++.h>
using namespace std;

int main() {
    array<long long, 2> negArr = {0, 0};
    array<long long, 3> posArr = {0, 0, 0};
    long long len, num; cin >> len;
    while (len--) {
        cin >> num;
        if (num >= 0) {
            if (posArr[0] < num) { posArr[2] = posArr[1]; posArr[1] = posArr[0]; posArr[0] = num; }
            else if (posArr[1] < num) { posArr[2] = posArr[1]; posArr[1] = num; }
            else if (posArr[2] < num) { posArr[2] = num;}
        }
        else {
            if (negArr[0] > num) { negArr[1] = negArr[0]; negArr[0] = num; }
            else if (negArr[1] > num) { negArr[1] = num; }
        }
    }
    
    cout << max(posArr[0]*posArr[1]*posArr[2], negArr[0]*negArr[1]*posArr[0]);
    return 0;
}
全部评论
你真无聊,这种想法真是卧龙
点赞 回复
分享
发布于 04-17 16:37 陕西

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务