博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HUAS Summer Trainning #3 B
阅读量:4322 次
发布时间:2019-06-06

本文共 1747 字,大约阅读时间需要 5 分钟。

题目:

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的序列,要你找到乘积最大的连续序列的积。(如果乘积小于0,就相当于乘积为0)

题目思路:既然要找连续的序列,给一个循环枚举起点,一个循环枚举终点,一个循环它起点到终点的元素乘起来,给个MAX变量赋值为0(因为乘积小于0点都会被赋值为0)。

X每个乘积都与MAX比较,比MAX大的,就把它赋值给MAX。循环完毕,输出MAX的值就行了。(还要注意输出案例时要连着输出2个换行)

代码:

1 #include
2 #include
3 #include
4 using namespace std; 5 const int maxn=18+5; 6 int main() 7 { 8 int n,i,a[maxn],j,k,p=0; 9 long int sum,max; 10 while(cin>>n&&n)11 { 12 for(i=0;i
>a[i];14 max=0;15 for(i=0;i
max)27 max=sum;28 }29 }30 printf("Case #%d: The maximum product is %lld.\n\n",++p,max);31 32 }33 return 0;34 }

 

转载于:https://www.cnblogs.com/huaxiangdehenji/p/4694124.html

你可能感兴趣的文章
Azure ARMTemplate模板,VM扩展命令
查看>>
第三周作业
查看>>
浅谈模块化
查看>>
(转)arguments.callee移除AS3匿名函数的侦听
查看>>
onNewIntent调用时机
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
学习新语言等技能的历程
查看>>
04代理,迭代器
查看>>
解决Nginx+PHP-FPM出现502(Bad Gateway)错误问题
查看>>
Java 虚拟机:互斥同步、锁优化及synchronized和volatile
查看>>
2.python的基本数据类型
查看>>
python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
查看>>
查看端口被占用情况
查看>>
浅谈css(块级元素、行级元素、盒子模型)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
PHP开源搜索引擎
查看>>
12-FileZilla-响应:550 Permission denied
查看>>
ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
查看>>
LeetCode 234. Palindrome Linked List
查看>>
编译HBase1.0.0-cdh5.4.2版本
查看>>