最大乘积连续子序列(数组)//枚举法的大好江山

问题 C: 最大乘积连续子序列(数组)
题目描述
输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列,如果这个最大乘积不是正数,则输出0。
输入
第一行输入n(1<=n<=9)表示序列的长度,第二行输入n个整数表示序列的元素(-10<=元素<=10)以空格分隔,最后一个数字之后无空格)。
输出
输出结果(结果之后无空格)。
样例输入
3
2 4 -3
样例输出
8

#include<stdio.h>
int main()
{
    int n,arr[20]={1};
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {   
        scanf("%d",&arr[i]);
    }
    int max=arr[0];
    for(int i=1;i<=n;i++)
    {
        /*i为连乘个数,j为开始位数*/
        for(int j=0;j<=n-i;j++)
        {
            int temp=1;
            for(int h=0;h<i;h++)
            {
                temp=temp*arr[j+h];
            }
            if(temp>max)
            {
                max=temp;

            }

        }
    }
    printf("%d\n",max);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40618238/article/details/78972913