现在的位置: 首页 > 综合 > 正文

7.1.2(最大乘积)

2013年08月11日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭

这个暴力法,还是比较好的不,但是一般在 比赛的过程中是遇不到的,,

不可能就是这种纯暴力的想法,但是也许就是个水题的话,自己还是可以做出来的不

呵呵,,还是不错的哈;;;

贴出代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

int a[22];

int max[22];

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
		}
		long long max = -99999999;
		for (int i = 0; i < n; i++)
		{
			long long pro = 1;
			for (int j = i; j < n; j++)
			{
				if (pro < pro * a[j])
				{
					pro = pro * a[j];
				}
			}
			if (max < pro)
			{
				max = pro;
			}
		}
		if (max < 0)
		{
			cout << "-1" << endl;
		}
		else
		{
			cout << max << endl;
		}
	}		
	system("pause");
	return 0;
}

抱歉!评论已关闭.