列表

详情


PDD8. 最大乘积

描述

给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)。
n<=1e5。

输入描述

第一行是数组大小n,第二行是无序整数数组A[n]

输出描述

满足条件的最大乘积

示例1

输入:

4
3 4 1 2

输出:

24

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2021-09-10

#include <stdio.h>

long long solve(int* A, int ALen ) {
    long long int max1=0,max2=0,max3=0,min1=0,min2=0;
    long long int num,temp,i;
    for(i=0;i<ALen;i++)
    {
        if(A[i]>max1)
        {
            max3=max2;
            max2=max1;
            max1=A[i];
        }
        else if(A[i]>max2)
        {
            max3=max2;
            max2=A[i];
        }
        else if(A[i]>max3)
        {
            max3=A[i];
        }
        if(A[i]<min1)
        {
            min2=min1;
            min1=A[i];
        }
        else if(A[i]<min2)
        {
            min2=A[i];
        }
    }
    if((max1*max2*max3)>(max1*min1*min2))
    {
        return max1*max2*max3;
    }
    else
    {
        return max1*min1*min2;
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int arr[n];
        int index=0;
        while(index<n)
        {
            scanf("%d",&arr[index++]);
        }
        printf("%lld\n",solve(arr,index));
    }
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 432KB, 提交时间: 2020-07-28

#include <stdio.h>
#include <string.h>
 
int main()
{
    int num, i, n;
    int maxArray[3] = { 1 };
    int minArray[2] = { 1 };
    int *ptr1 = maxArray;
    int *ptr2 = minArray;
    scanf("%d", &num);
    if ((ptr1 != NULL) && (ptr2 != NULL))
    {
        for (i = 0; i < num; i++)
        {
            scanf("%d", &n);
            if (n > ptr1[0])
            {
                ptr1[2] = ptr1[1];
                ptr1[1] = ptr1[0];
                ptr1[0] = n;
            }
            else if (n > ptr1[1])
            {
                ptr1[2] = ptr1[1];
                ptr1[1] = n;
            }
            else if (n > ptr1[2])
            {
                ptr1[2] = n;
            }
            else if (n < ptr2[0])//表示比最小值还小
            {
                ptr2[1] = ptr2[0];
                ptr2[0] = n;
            }
            else if (n < ptr2[1])
            {
                ptr2[1] = n;
            }
        }
         long result1 = (long)ptr1[0] * (long)ptr1[1] * (long)ptr1[2];
         long result2 = (long)ptr1[0] * (long)ptr2[0] * (long)ptr2[1];
         long output = result1 > result2 ? result1 : result2;
        printf("%ld", output);
        return 0;
    }
}

C 解法, 执行用时: 3ms, 内存消耗: 360KB, 提交时间: 2019-07-03

#include <stdio.h>
#include <string.h>

int main()
{
	int num, i, n;
	int maxArray[3] = { 1 };
	int minArray[2] = { 1 };
	int *ptr1 = maxArray;
	int *ptr2 = minArray;
	scanf("%d", &num);
	if ((ptr1 != NULL) && (ptr2 != NULL))
	{
		for (i = 0; i < num; i++)
		{
			scanf("%d", &n);
			if (n > ptr1[0])
			{
				ptr1[2] = ptr1[1];
				ptr1[1] = ptr1[0];
				ptr1[0] = n;
			}
			else if (n > ptr1[1])
			{
				ptr1[2] = ptr1[1];
				ptr1[1] = n;
			}
			else if (n > ptr1[2])
			{
				ptr1[2] = n;
			}
			else if (n < ptr2[0])//表示比最小值还小
			{
				ptr2[1] = ptr2[0];
				ptr2[0] = n;
			}
			else if (n < ptr2[1])
			{
				ptr2[1] = n;
			}
		}
		 long result1 = (long)ptr1[0] * (long)ptr1[1] * (long)ptr1[2];
		 long result2 = (long)ptr1[0] * (long)ptr2[0] * (long)ptr2[1];
		 long output = result1 > result2 ? result1 : result2;
		printf("%ld", output);
		return 0;
	}
}

C 解法, 执行用时: 3ms, 内存消耗: 364KB, 提交时间: 2019-04-18

#include<stdio.h>
#include<string.h>
 
int a=-2147483648,b=-2147483648,c=-2147483648,n,x,d=2147483647,e=2147483647;
void cmp(int x) {
 
        if(x>a){
            c=b;
            b=a;
            a=x;
        }
        else{
            if(x>b){
            c=b;
            b=x;
            }
            else{
                if(x>c){
                c=x;
                }
            }
             
        }
        if(x<e){
            d=e;
            e=x;
        }
        else{
            if(x<d){
                d=x;
            }
        }
        return;
     
     
}
int main(){
    int i,j,k;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&x);
        cmp(x);
    }
    if((long long )a*b*c>(long long)a*d*e)
        printf("%lld",(long long)a*b*c);    
    else
        printf("%lld",(long long)a*d*e);    
}

C 解法, 执行用时: 3ms, 内存消耗: 368KB, 提交时间: 2021-01-05

#include<stdio.h>
#include<limits.h>

int main()
{
    int n;
    long int num;
    long long int value1, value2;
    long int maxA = INT_MIN, maxB = INT_MIN, maxC = INT_MIN;
    long int minA = INT_MAX, minB = INT_MAX;
    
    scanf("%d", &n);
    getchar();
    
    for (int i = 0; i < n; i++)
    {
        scanf("%ld ", &num);
        if (num > maxA)
        {
            maxC = maxB;
            maxB = maxA;
            maxA = num;
        }
        
        else if (num > maxB)
        {
            maxC = maxB;
            maxB = num;
        }
        
        else if (num > maxC)
            maxC = num;
        
        if (num < minA)
        {
            minB = minA;
            minA = num;
        }
        
        else if (num < minB)
            minB = num;  
    }
    
    value1 = maxA * maxB * maxC;
    value2 = minA * minB * maxA;
    
    if (value1 > value2)
        printf("%lld", value1);
    
    else
        printf("%lld", value2);
    
    return 0;
}

上一题