Java解题-杭电OJ-1003题
来源:互联网 发布:单片机usb3.3v接哪里 编辑:程序博客网 时间:2024/05/24 00:23
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 222921 Accepted Submission(s): 52449
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6
import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int cas=s.nextInt(); for (int i = 1; i <= cas; i++) { int n=s.nextInt(); int sum = 0, max = -0xfffffff; int start=1, last=1, temp = 1; for (int j= 1; j<= n; j++) { int a = s.nextInt(); sum += a; if (sum > max) { max = sum; start = temp; last = j; } if (sum < 0) { temp = j+ 1; sum = 0; } } System.out.println("Case "+i+":"); System.out.println(max+" "+start+" "+last); if(i!=cas) System.out.println(); } }}
0 0
- Java解题-杭电OJ-1003题
- Java解题-杭电OJ-1000题
- Java解题-杭电OJ-1001题
- Java解题-杭电OJ-1002题
- Java解题-杭电OJ-1004题
- 杭电oj 1003 java
- 杭电OJ 1209题clock 解题报告
- 杭电OJ题 1106 排序 解题报告
- 杭电OJ 题 1209 Clock 解题报告
- 杭电OJ题 1236 排名 解题报告
- 杭电OJ题 1391 Number Steps 解题报告
- 杭电OJ题 1347 Grandpa is Famous解题报告
- 杭电OJ题 1361 Parencodings解题报告
- 杭电OJ题 1379 DNA Sorting解题报告
- 杭电OJ题 1390 Binary Numbers解题报告
- 杭电OJ题 1393 Weird Clock解题报告
- 杭电OJ题 Goldbach's Conjecture解题报告
- 杭电OJ题 1408 盐水的故事 解题报告
- CodeForces 722C. Destroying Array(逆向思维)
- ADC_RegularChannelConfig 的参数 Rank的含义
- C#——基于委托与事件的多线程通信(同样适用于非UI线程间通信)
- 字符编码的历史总结,UTF8的编码规则,文件BOM的由来,笔记
- Tracking-Learning-Detection原理分析
- Java解题-杭电OJ-1003题
- C#的36课的主要内容
- C语言入门级教程
- hdu 1010 回溯加奇偶性剪枝
- 多线程和并发
- ubuntu下查看和配置串口 转自http://shower.0fees.us
- Android中SQLite使用拓展
- windows 环境下Vapoursynth安装和配置
- SVM分类器