UVA - 11059 Maximum Product

来源:互联网 发布:登录新熊片数据库 编辑:程序博客网 时间:2024/06/10 23:15


这题用枚举最简单啦,如果想用背包的话情况就会变得很复杂,毕竟加法不同于乘法

#include <iostream>
using namespace std;
const int N = 25;
typedef long long LL;
int book[N];
LL judge(int x, int y);


int main()
{
    int n, ncase=0;;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>book[i];
        }
        LL ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)
            {
                ans=max(ans,judge(i,j));
            }
        }
        cout<<"Case #"<<++ncase<<": The maximum product is "<<ans<<"."<<endl<<endl;
    }
    return 0;
}


LL judge(int x, int y)
{
    LL sum=1;
    for(int i=x;i<=y;i++)
    {
        sum*=book[i];
    }
    return sum;
}

0 0
原创粉丝点击