博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA11059 Maximum Product
阅读量:6983 次
发布时间:2019-06-27

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

问题链接:。基础级练习题,用C语言编写程序。

题意简述:输入n个整数序列,有正有负,求这个序列中最大连续累乘的子序列,其最大的值为多少。如果结果为负数,则输出0。

问题分析:如果整数序列中有0,则用0分段然后分别计算。对于每个分段(可能只有一个分段),其中没有0,如果其中有偶数个负数,则将分段中所有的数相乘就是所求结果。如果分段中有奇数个负数,那么最大的累乘出现在第1个负数的右边开始的子序列或从开始到最后1个负数左边的子序列。

程序说明:(略)

AC的C语言程序如下:

/* UVA11059 Maximum Product */#include 
int main(void){ int n, val, caseno=0, flag; long long ans, max, afternegativemax; while(scanf("%d", &n) != EOF) { ans = 0; max = 1; afternegativemax = 1; flag = 0; while(n--) { scanf("%d", &val); if(val == 0) { max = 1; afternegativemax = 1; flag = 0; } else { max *= val; if(max > ans) ans = max; if(flag) { afternegativemax *= val; if(afternegativemax > ans) ans = afternegativemax; } if(val < 0) flag = 1; } } printf("Case #%d: The maximum product is %lld.\n\n", ++caseno, ans); } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564490.html

你可能感兴趣的文章
Maven构建springMVC+spring+MyBatis项目
查看>>
猴子选大王
查看>>
3249 搭积木
查看>>
POJ2749:Building roads——题解
查看>>
[SpringMVC]定义多个前缀映射的问题
查看>>
高中时的口头禅
查看>>
C++ 虚函数表解析
查看>>
[SCOI2009]windy数
查看>>
Struts2--Action属性接收参数
查看>>
2012年科技新闻背后的大数字
查看>>
报价单内,同一物料只允许一条行价格记录
查看>>
leetcode 283. Move Zeroes
查看>>
自己的php函数库
查看>>
HDU Problem 1599 find the mincost route 【Floyd求最小环】
查看>>
HDU2017多校联合 contest 1
查看>>
基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理(转载自:http://www.cnblogs.com/wuhuacong/p/4175266.html)...
查看>>
range
查看>>
[Noi2002]Savage 题解
查看>>
特征选择, 经典三刀(转)
查看>>
【Python3爬虫】自动查询天气并实现语音播报
查看>>