uva11059(最大乘积)
Posted zsychanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva11059(最大乘积)相关的知识,希望对你有一定的参考价值。
Problem D - Maximum Product
Time Limit: 1 second
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.
思路:
输入n个元素组成的序列S,你须要找一个成绩最大的连续子序列,假设最大子序列为负数。输出0.
思路:
确定七点和终点,进行枚举,然后推断符不符合要求。
代码:
#include<cstdio> #include<algorithm> using namespace std; int S[20]; int main() { int n,casex=1; long long ans,a; while(scanf("%d",&n)!=EOF) { ans=0; for(int i=1;i<=n;i++) scanf("%d",&S[i]); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { a=1; for( int k=i;k<=j;k++) { a*=S[k]; } ans=max(ans,a); } } printf("Case #%d: The maximum product is %lld.\n\n",casex,ans); casex++; } return 0; }
以上是关于uva11059(最大乘积)的主要内容,如果未能解决你的问题,请参考以下文章