读入input.txt数组求其子数组最大和
package qiuhe;
public class qiuhe {
public static void main(String args[]) {
// TODO Auto-generated method stub
int matrix[][]={{1000000000,-2000000000,300001560},{-4,1051010101,6},{-7,800000000,-9}};
maxSum(matrix);
}
public static void maxSum(int matrix[][])
{
if(matrix==null||matrix.length==0)
return;
int max=0;
int col=matrix[0].length,row=matrix.length;
for(int i=0;i<row;i++)
{
int arr[]=new int[col];
for(int j=i;j<row;j++)
{
//遍历所有的子行
for(int k=0;k<col;k++)
{
arr[k]+=matrix[j][k];
//将每子行的值进行相加然后利用子数组的最大和就可以求出子矩阵的最大和
}
for(int l=0;l<col;l++) {
if((l+1)%3==0) {
System.out.println();
}
}
max=Math.max(max(arr), max);
//求出数组的子数组和最大值
}
}
System.out.println("最后得到的最大值为:"+max);
}
public static int max(int arr[])//求最大子数组之和
{
int max=0,sum=0;
for(int i=0;i<arr.length;i++)
{
if(sum<=0)
{
sum=arr[i];
}
else {
sum+=arr[i];
}
max=Math.max(sum, max);
}
return max;
}
}
读入txt文档进行最大子数组求和输出最大值
成员
庄树琦 许新达
许新达:提供思路 找bug
庄树琦:写代码

浙公网安备 33010602011771号