例题7-2 最大乘积(Maximum Product, UVa 11059)

原题链接:https://vjudge.net/problem/UVA-11059
分类:暴力求解
备注:枚举连续子序列

代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 20;
ll a[maxn], kase;
int main(void) {
	int n;
	while (~scanf("%d", &n)) {
		ll ans = 0;
		for (int i = 0; i < n; i++)
			scanf("%lld", &a[i]);
		for (int i = 0; i < n; i++) 
			for (int j = i; j < n; j++) {
				ll sum = 1;
				for (int k = i; k <= j; k++) 
					sum *= a[k];
				ans = max(ans, sum);
			}
		printf("Case #%lld: The maximum product is %lld.\n\n", ++kase, ans);
	}
	return 0;
}
发布了104 篇原创文章 · 获赞 97 · 访问量 4515

猜你喜欢

转载自blog.csdn.net/TK_wang_/article/details/105328394