UVa 11059 - Maximum Product 最大乘积【暴力】

题目链接:https://vjudge.net/contest/210334#problem/B

题目大意:
Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you should consider 0 as the value of the maximum product.

Input

Each test case starts with 1 ≤ N ≤ 18, the number of elements in a sequence. Each element Si is an integer such that −10 ≤ Si ≤ 10. Next line will have N integers, representing the value of each element in the sequence. There is a blank line after each test case. The input is terminated by end of file (EOF).

Output

For each test case you must print the message: ‘Case #M: The maximum product is P.’, where M is the number of the test case, starting from 1, and P is the value of the maximum product. After each test case you must print a blank line.

Sample Input

3

2 4 -3

5

2 5 -1 2 -1

Sample Output

Case #1: The maximum product is 8.

Case #2: The maximum product is 20.

即求数字的连续乘积最大值。

#include <cstdio>
#include <iostream>
using namespace std;
#define ll long long
#define INF -0x3f3f3f3f
int n;
;
];

int main()
{
    while (cin >> n)
    {
        int i, j;
        ; i <= n; i++)cin >> arr[i];
        ll maxn = INF;
        ; i <= n; i++)      //以该坐标为起点,开始乘它后面的数
        {
            ll sum = ;
            ; i+j<=n; j++)  //表示i要向后乘几位
            {
                sum *= arr[i+j];      //注意这里是i+j,要仔细体会,刚开始就是wrong在这里了
                if (sum > maxn)maxn = sum;    //每向后多多乘一位,都要和最大值比较一下,看能不能更新最大值
            }
        }
        )
        {
            cout << "Case #" << ++cas << ": The maximum product is " << maxn <<'.'<< endl<<endl;
        }
        else
        {
            cout << "Case #" << ++cas << ": The maximum product is 0." << endl<<endl;
        }
    }
    ;
}

2018-04-10

上一篇:java编译需要文件后缀名.java 而运行不需要后缀名.class


下一篇:Android Programming: Pushing the Limits -- Chapter 3: Components, Manifests, and Resources