nyoj 44 子串和
来源:互联网 发布:daemontools linux 编辑:程序博客网 时间:2024/05/22 17:31
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=44
这个题做过好久了,但是一直没有记录下来。。。好吧,开始!
题目大意:给一个整形数组,求连续非空子数组的最大和。
解题思路:这题,最重要的就是一个思路,用一个max变量始终记录连续最大子数组和,sum记录当前和,而sum的处理是最重要的!
上码更清楚!
import java.io.BufferedInputStream;public class Main{static BufferedInputStream bis = new BufferedInputStream(System.in);public static void main(String[] args) throws Exception {int n, sum, max, nCase;nCase = getInt();// 对输入整数的优化while (nCase-- != 0) {n = getInt();int[] arr = new int[n];for (int i = 0; i < n; i++) {arr[i] = getInt();}max = sum = arr[0];for (int i = 1; i < n; i++) {if (sum < 0) {//逻辑处理sum = arr[i];} else {sum += arr[i];}max = Math.max(max, sum);//!!max始终记录最大的子序列和}System.out.println(max);}bis.close();}// 用BufferedInputStream的read()方法来减少读入的时间static int getInt() throws Exception {int i;// ' ':32 '\t':9 '\n':10 '-':45 '0':48// 除去间隔符while ((i = bis.read()) < 45);int temp = 0, mark = 1;if (i == 45) {//读入的是负号mark = -1;i = bis.read();}while (i > 47) {temp = temp * 10 + i - 48;i = bis.read();}return mark * temp;}}
0 0
- nyoj 44 子串和
- NYOJ-子串和44
- NYOJ 44 子串和
- nyoj-44-子串和
- NYOJ 44 子串和
- NYOJ 44 子串和
- NYOJ 44 子串和
- NYOJ 44 子串和
- NYOJ 44 子串和
- nyoj 44 子串和
- nyoj 44 子串和
- NYOJ--44子串和
- NYOJ 子串和 44
- NYOJ 44【子串和】
- nyoj 44 子串和
- 子串和 nyoj 44
- NYOJ 44 子串和
- nyoj 44 子串和
- 从零开始利用XMPP做即时通讯ios篇(一)
- 使用Memcached、Spring AOP构建数据库前端缓存框架
- 谷丽萍年轻时真是个倾国倾城的大美女(图)
- 推荐!国外程序员整理的Java资源大全
- jquery.min.js一引入到工程中显示红叉(总结)
- nyoj 44 子串和
- MySQL 的skip_name_resolved 模式解决远程访问MySQL慢问题,以及添加后warning解决办法
- ubuntu 上airodump-ng显示无线信号强度
- nyoj 102 最大和
- java反射读取字符创建类是出现 java.lang.ClassNotFoundException 错误
- 不知为啥有时atheros 9344的MAC会挂死
- nyoj1170 最大的数
- MFC 如何将自定义按钮,添加到对话框的成员变量中?
- MFC 列表控件CListCtrl如何自动显示到最后一屏?