//求最大子序列和问题,
public class MaxSubSum{
public static int maxSubSum(int[] a){
int maxSum = 0, thisSum = 0;
for(int j = 0; j < a.length; j++){
thisSum += a[j];
if(thisSum > maxSum)
maxSum = thisSum;
else if(thisSum < 0)
thisSum = 0;
}
return maxSum;
}
public static void main(String[] args){
int[] b = {-2,11,-4,13,-5,-2};
int max = maxSubSum(b);
System.out.println(max);
}
}
求最大子序列和问题的最优算法
最新推荐文章于 2024-10-30 16:47:45 发布