UVa 11059 Maximum Product

Description

N个数,求最大连续元素乘积

Algorithm

枚举开头结尾

Hint

单个枚举 i.e. 开头 == 结尾
要用Long long

Code

#include <cstdio>
#include <iostream>
using namespace std;
int n;
const int maxn = 18 + 9;
int a[maxn];
void solve()
{
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
    {
      long long s = 1;
      for (int k = i; k <= j; k++)
        s *= a[k];
      if (s > ans) ans = s;
    }
  cout << ans << ".\n";
  cout << endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
  int t = 0;
  while (cin >> n)
  {
    t++;
    cout << "Case #" << t << ": The maximum product is ";
    solve();
  }
}

你可能感兴趣的:(UVa 11059 Maximum Product)