先贴代码
“`
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int j;
int[] a=new int[10000];
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int m,p,q;
int sum;
int l,r,max;
for (int i = 0; i < n; i++) {
l=0;
r=0;
m=scanner.nextInt();
sum=0;
for (p=0,sum=0,j = 0,max=-9999; j < m; j++) {
q=scanner.nextInt();
sum+=q;
if(max<sum)
{
l=p;
r=j;
max=sum;
}
if(sum<0)
{
p=j+1;
sum=0;
}
}
int temp=i+1;
l++;
r++;
System.out.println("Case "+temp+":");
System.out.println(max+" "+l+" "+r);
if (temp<n) {
System.out.println();
}
}
scanner.close();
}
}
在写的中间发现用数组的方式不能通过,应该是数组越界的问题。