题目简单,仅供记录使用。
最大子序列和。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
int count = sc.nextInt();
for (int i = 0; i < count; i++)
{
int size = sc.nextInt();
int[] nums = new int[size];
for(int j = 0; j < size; j++)
{
nums[j] = sc.nextInt();
}
int[] dp = new int[size];
int[] begin = new int[size];
dp[0] = nums[0];
begin[0] = 0;
for(int x = 1; x < size; x++)
{
if(nums[x] <= nums[x] + dp[x - 1])
{
dp[x] = nums[x] + dp[x - 1];
begin[x] = begin[x - 1];
}else
{
dp[x] = nums[x];
begin[x] = x;
}
}
int max = dp[0];
int max_index = 0;
for (int j = 1; j < size; j++)
{
if (dp[j] > max)
{
max = dp[j];
max_index = j;
}
}
int h = begin[max_index] + 1;
int t = max_index + 1;
int cases = i + 1;
System.out.println("Case " + cases + ":");
System.out.println(max + " " + h + " " + t);
if(i != count - 1 )
System.out.println();
}
}
}
}