杭电acm 1003提交一直是wrong answer!!求大神帮忙,刚接触acm
本帖最后由 chen820655096 于 2013-02-09 00:15:33 编辑 题目要求:
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
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
import java.io.BufferedReader;java acm
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main{
public static void main(String []args){
//下面的这种试读取速度比较快。
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int a=0;
a=sc.nextInt();//a为输入的样例数
int reslt[][]=new int[a][2];//存放结果
int s[][]=new int [a][30];//存放输入的数
for(int i=0;i<a;i++){
int b=0,sum=0;
b=sc.nextInt();
int sum1[]=new int[b];
sum=0;
for(int j=0;j<b;j++){
s[i][j]=sc.nextInt();
sum=sum+s[i][j];
sum1[j]=sum;
}
reslt[i][0]=maxsum(sum1);//存放最后的结果
reslt[i][1]=sum1[0];
}
for(int m=0;m<a;m++){
System.out.println("Case "+(m+1)+":");
System.out.println(reslt[m][1]+" "+1+" "+(reslt[m][0]+1));
if(m!=(a-1))
System.out.println();
}
}
public static int maxsum(int a[]){
int max=a[0],b=0;
for(int i=0;i<a.length;i++){
if(i+1<a.length){
if(a[i]<a[i+1]){
max=a[i+1];
b=i+1;
}
}
}
a[0]=max;
return b;
}
}
import java.util.Scanner;
public class AcmTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int caseNum = scanner.nextInt();
scanner.nextLine();
int[][] input = new int[caseNum][];
for (int i = 0; i < caseNum; i++) {
int length = scanner.nextInt();
input[i] = new int[length];
for (int j = 0; j < length; j++) {
input[i][j] = scanner.nextInt();
}
scanner.nextLine();
}
for (int i = 0; i < caseNum; i++) {
System.out.println("Case " + (i + 1) + ":");
outputCase(input[i]);
System.out.println();
}
}
public static void outputCase(int[] a) {
int sum = a[0];
int start = 0;
int end = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; i + j < a.length; j++) {
int tempSum = 0;
for (int k = 0; k <= j; k++) {
tempSum += a[i + k];
}
if (tempSum > sum) {
sum = tempSum;
start = i + 1;
end = i + j + 1;
}
}
}
System.out.println(sum + " " + start + " " + end);
}
}