一维数组首尾相连,两个数字可以控制一个子数组。

 

 

package mypackage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Scanner; 

public class d {
    public static int max(int a[],int n) //判断大小
    {
        int x=a[0];
        for(int i=0;i<=n;i++)    
        {
            if(a[i]>x) {x=a[i];}                
        }
        return x;
    }
    
    public static int a(int a[],int n)
    {
        int i,j,p=0;
        int[] b=new int [1000];
        for(i=0;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                b[p]=b(a,i,j,n);
                p++;
            }
        }        
        return max(b,p-1);
    }
    
    public static int b(int a[],int i,int j,int n)
    {
        int x,y,p=a[i],q=a[0];
        for(x=i+1;x<=j;x++)
        {
            p=p+a[x];        
        }
        if(i!=j)
        {
            for(y=1;y<=i;y++) 
            {
                q=q+a[y];                
            }
            for(y=j;y<=n;y++)
            {
                q=q+a[y];
            }
        }
        if(q>=p) {p=q;}
        return p;
    }

    public static void main(String[] args) throws IOException {
        Reader r = new BufferedReader(new FileReader("C:\\\\\\\\Users\\\\\\\\xxoo\\\\\\\\Desktop\\\\\\\\新建文本文档.txt"));
        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        int[] sum=new int [1000];
        int i=-1;
        stok.nextToken();
        while (stok.ttype != StreamTokenizer.TT_EOF) {
          if (stok.ttype == StreamTokenizer.TT_NUMBER)
          {  
              i++;
            sum[i] = (int) stok.nval;
          }
          else
            System.out.println("Nonnumber: " + stok.sval);
          stok.nextToken();
        }//读取
        System.out.println(a(sum,i));
    
    }
}

 二维数组四个数字可以控制一个矩形。

package mypackage;
import java.util.Scanner; 
public class c {
    public static int a(int c[][],int i,int j) 
    {
        int m,n,x,y,p=0;
        int[] sum=new int [1000];
        for(m=0;m<=i;m++)
        {
            for(n=m;n<=i;n++)
            {
                for(x=0;x<=j;x++)
                {
                    for(y=x;y<=j;y++)
                    {
                        sum[p]=b(c,m,n,x,y);
                        p++;
                    }
                }
            }
            
        }
        
        return max(sum,p);
    }
    
    public static int b(int b[][],int m,int n,int x,int y)
    {
        int a=b[m][x];
        for(int i=m;i<=n;i++)
        {
            for(int j=x;j<=y;j++)
            {
                a=a+b[i][j];
            }
        }
        return a-b[m][x];
    }
    
    public static int max(int a[],int n) //判断大小
    {
        int x=a[0];
        for(int i=0;i<=n;i++)    
        {
            if(a[i]>x) {x=a[i];}                
        }
        return x;
    }
    
    
    public static void main(String[] args){
        int[][] a=new int [1000][1000];
        int x,y;

        Scanner sc2 = new Scanner(System.in); 
        Scanner sc3 = new Scanner(System.in); 
        System.out.println("请输入行数和列数:");
        x=sc2.nextInt(); 
        y=sc3.nextInt(); 
        System.out.println("请输入数组:");
        Scanner sc1 = new Scanner(System.in);
        for(int i=0;i<=x-1;i++)
        {
            for(int j=0;j<=y-1;j++)
            {       
                
                a[i][j]= sc1.nextInt(); 
            }
        }
        System.out.println(a(a,x-1,y-1));

    }
}