标签:java
/** * 5 -1 -1 -1 -1 -1 * 7 0 6 -1 1 -6 7 -5 * 5 6 -1 5 4 -7 * G和最大的子序列,输出最大和还有起始与截止下标(暴力解法,超时了Time Limit Exceeded) * @param args the command line arguments */ public static void main(String[] args) { int N; Scanner cin = new Scanner(System.in); N = cin.nextInt(); for(int k =1; k <=N; k++) { int[] a = new int[1000000]; int num = cin.nextInt(); a[0] = num; int length = 1; while(length <= a[0]){ num = cin.nextInt(); a[length] = num; length ++; } length = a[0]; int indexBegin = 1; int indexNext = 1; int sum = a[1]; for(int j = 1; j <= length; j++ ) { int temp = 0; int tempSum = 0; for(int i = j; i <= length; i++) { temp = tempSum; tempSum = tempSum + a[i]; if((tempSum >= temp) && (tempSum > sum)){ sum = tempSum; indexBegin = j; indexNext = i; } } } System.out.println("Case " + k +‘:‘ ); System.out.println( sum + " " +indexBegin + " "+indexNext); } }
标签:java
原文地址:http://11543608.blog.51cto.com/11533608/1795734