最大乘积
2014-03-14 14:35
225 查看
/* 输入那个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个 最大乘积不是正数,应输出-1(表示无解)。1<=n<=18 , -10<=Si<=10 * 样例输入: 3 2 4 -3 5 2 5 -1 2 -1 样例输出: 8 20 * */ import java.util.*; public class Main6 { public static int n; public static int[] array; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while (sc.hasNext()) { n = sc.nextInt(); array = new int ; long[] tmp = new long ; // 初始化保存各个起点和终点的乘积 for (int i = 0; i < n; i++) { tmp[i] = 1; } // 读数和计算乘积 for (int i = 0; i < n; i++) { array[i] = sc.nextInt(); if (i > 0) tmp[i] *= tmp[i - 1] * array[i]; else tmp[i] = array[i]; } long max = tmp[0]; // 遍历数组,找出乘积最大的数 for (int i = 1; i < n; i++) { max = max > tmp[i] ? max : tmp[i]; } System.out.println(max); } } }
相关文章推荐
- 如何使用 docker 及常用命令(一)
- IOS 获得网络图片
- Create view failed with ORA-01031:insufficient privileges
- js日期重写
- Mybatis与ibatis的对比
- Java 几种经典排序算法
- 使用ListView的addFooterView或者addHeaderView报错或者不知名的异常:at android.widget.ListView.clearRecycledState(List
- struts2自学学习笔记之一
- JavaScript中按位“异或”运算符
- 云脉CC慧眼百度搜索版,超强大的图文转换,快速翻译软件
- Linux学习记录--shell script
- wandboard-sd启动制作链接
- 不支持绑定子目录也没关系,更改.htaccess文件实现子目根绑定
- PowerShell学习(二),筛选与输出格式
- 几个不常见却超实用的PHP函数
- XmlSerializer. Deserialize解析xml
- Balloon.2.6 —— UI组件之弹出窗
- SPI协议及其工作原理浅析(转)
- alert乱码问题
- 使用RenderControl方法把GridView控件导出到EXCEL