暴力求解最大乘积

题意:

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.

思路分析:

这道题关键在于给出计算的起点和终点。然后一直循环计算,最后再在所有的乘积中找到最大的数就KO了!

注意:

1、输出格式,空行

2、数字比较大,用long long类型

源代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     int n;
 9     int count = 0;
10     while (scanf_s("%d",&n)!=EOF)            //输入方式
11     {
12         count++;
13         int a[25];
14         for (int i = 0; i < n; i++)
15             cin >> a[i];
16
17         long long sum = 0;
18         long long m = 0;
19         for (int i = 0; i < n; i++)        //起始点
20         {
21             for (int j = i; j < n; j++)        //终点
22             {
23                 sum = 1;                           //每次一回合要重新开始
24
25                 for (int k = i; k <= j; k++)
26                 {
27                     sum *= a[k];
28                     m = max(sum, m);                //找最大的数
29                 }
30             }
31         }
32
33     printf("Case #%d: The maximum product is %lld.\n\n", count, m);
34     }
35     return 0;
36 }

心得:

做完这个题目心得还真没有,总之把代码写得让别人看得懂就好。题目用了三个循环,前两个是给出起点跟终点,第三个是计算乘积,每次出来一个乘积,就用max函数保存最大的数。

时间: 2024-08-05 07:07:12

暴力求解最大乘积的相关文章

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

求解最大子数组问题 -- 暴力求解 和 分治法求解

/*------------------ 求解最大子数组问题 --------------- 最大子数组,就是求解一个数组的所有元素的各种组合中,和最大的 那个子数组.在这种情况下,如果元素值全部非负,那么最大子数组当然 是所有元素.但是如果有负值的元素存在,那么久需要找到一个由数组中 连续几个元素组成的子数组并且其元素值之和最大. */ #include <iostream> using namespace std; struct ArrayStruct { ArrayStruct(int

BZOJ 1054题解 BFS暴力求解

BZOJ 1054题解 BFS暴力求解 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1884  Solved: 1033[Submit][Status][Discuss] Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移 动到某人

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerou

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655

HDU 2601An easy problem-素数的运用,暴力求解

An easy problem Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description When Teddy was a child , he was always thinking about some simple math problems ,such as "What it's 1 cup of water plus 1 pile o

ZOJ 2856 Happy Life 暴力求解

因为是Special Judge 的题目,只要输出正确答案即可,不唯一 暴力力求解, 只要每次改变 happiness 值为负的人的符号即可. 如果计算出当前人的 happiness 值为负,那么将其 p(i) 值赋值为-p(i) 即可这题是保证有解的,至至于为何难以证明. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #incl

求最大子数组的和,算法导论只分治递归求解,暴力求解,记忆扫描方法。

#include<iostream> #include<vector> using namespace std; /*******************************************************************************************/ // 分治方法,利用递归的思想 // ugly_chen 2014.11.3 22:24 //说明:把数组分为两部分,右边部分和左边部分,要不就是右边部分和左边部分之和. // ---