分两步
1.定义一个函数,参数是i,表示求以si开头的子串的最大乘积
2.分别以i为0,1,2,,3.。。。。。n 调用该函数, 然后在这些乘积中选出最大值,就是所求。
#include <iostream>
using namespace std;
const int maxn = 18 + 5;
int s[maxn];
int n;
long long _max(int i) //计算数组中从i出发的乘积最大的子串的积
{
long long product = 1, maxi = 0;
for(int j = i; j < n; j++)
{
product *= s[j];
if(product > maxi)
maxi = product;
}
return maxi;
}
int main()
{
int Case = 1;
while(cin >> n)
{
for(int i = 0; i < n; i++)
cin >> s[i];
long long result = 0;
for(int i = 0; i < n; i++)
{
long long each_max = _max(i);
if(each_max > result)
result = each_max;
}
cout<<"Case #"<<Case<<": The maximum product is "<<result<<"."<<endl<<endl;
Case++;
}
}