程序要使用的数组放在一个叫 input.txt 的文件中, 文件格式是:

数组的行数,

数组的列数,

每一行的元素, (用逗号分开)

每一个数字都是有符号32位整数,当然,行数和列数都是正整数。

5 8 2
7 9 6

初步的想法是成块相加通过循环得出结果

及(1,1)+(1,2)和(1,1)+(1,2)+(1,3)+(1,n)比较

之后比较(1,1)+(2,1)+(n,1)

然后比较(1,1)+(1,2)+(2,1)+(2,2)+(1,n)+(n,1)+(n,n)

即:

点、行、块

package geren_04;
class D//点
{
    private int num;
    private int x;
    private int y;
    public D(){};
    public D(int num,int x,int y)
    {
        this.num=num;
        this.x=x;
        this.y=y;
    }
    public D(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public void show()
    {
        System.out.println("("+x+" , "+y+" ) : "+num);
    }
}
 
package geren_04;
import java.util.ArrayList;
public class H //行
{
    private int sum;
    private D Z;
    private D Y;
    private ArrayList<D> h;
    public H() {}
    public H(D Z,D Y)
    {
        this.Z=Z;
        this.Y=Y;
    }
    public String toString()
    {
        String s="";
        s+="# \n( "+Z.getX()+" , "+Z.getY()+" ) to ( "+Y.getX()+" , "+Y.getY()+" )";
        for(int i=0;i<h.size();i++)
        {
            s+=h.get(i)+" ";
        }
        s+="#\n";
       
        return s;
    }
    public void setValue(ArrayList<D> h)
    {
        this.h=h;
    }
    public int getSum()
    {
        D d=new D();
        sum=0;
        for(int i=0;i<h.size();i++)
        {
            d=h.get(i);
            sum+=d.getNum();
        }
        return sum;
    }
    public int getD(int i)
    {
        return h.get(i).getNum();
    }
    public void show()
    {
        System.out.println("# \n( "+Z.getX()+" , "+Z.getY()+" ) to ( "+Y.getX()+" , "+Y.getY()+" )");
        for(int i=0;i<h.size();i++)
        {
            System.out.print(h.get(i).getNum()+" ");
        }
        System.out.println("\n#\n");
    }
    public D getZ() {
        return Z;
    }
    public void setZ(D z) {
        Z = z;
    }
    public D getY() {
        return Y;
    }
    public void setY(D y) {
        Y = y;
    }
    public void setSum(int sum) {
        this.sum = sum;
    }
   
}
 

 

package geren_04;
import java.util.ArrayList;
class K//块
{
    private int flag;
    //左上顶点
    private D ZS;
    //右上顶点
    private D YS;
    //左下顶点
    private D ZX;
    //右下顶点
    private D YX;
    private int l;//长-横着
    private int w;//宽-竖着
    private int sum;
    private ArrayList<H> k=new  ArrayList<H>();
    public K() {l=0;w=0;}
    public K(D ZS,D YS, D ZX, D YX)
    {
        this.ZS=ZS;
        this.YS=YS;
        this.ZX=ZX;
        this.YX=YX;
        this.l=YS.getX()-ZS.getX();
        this.w=ZX.getY()-ZS.getY();
    }
    public void setValue(ArrayList<H> k)
    {
        this.k=k;
    }
    public int getSum()
    {
        sum=0;
        for(int i=0;i<w;i++)
        {
            sum+=k.get(i).getSum();
        }
        return sum;
    }
    public void show()
    {
        H h=new H();
        System.out.println("#\n( "+ZS.getX()+" , "+ZS.getY()+" ) to ( "+YS.getX()+" , "+YS.getY()+" )");
        System.out.println("( "+ZX.getX()+" , "+ZX.getY()+" ) to ( "+YX.getX()+" , "+YX.getY()+" )");
        for(int i=0;i<w;i++)
        {
            for(int t=0;t<l;t++)
            {
                System.out.print(k.get(i).getD(t)+"\t");
            }
            System.out.println("");
        }
        System.out.println("#\n");
    }
   
    public D getD(int x,int y)
    {
        D d=new D();
        d.setX(x);
        d.setY(y);
        d.setNum(k.get(y).getD(x));
        return d;
    }
    public D getZS() {
        return ZS;
    }
    public void setZS(D zS) {
        ZS = zS;
    }
    public D getYS() {
        return YS;
    }
    public void setYS(D yS) {
        YS = yS;
    }
    public D getZX() {
        return ZX;
    }
    public void setZX(D zX) {
        ZX = zX;
    }
    public D getYX() {
        return YX;
    }
    public void setYX(D yX) {
        YX = yX;
    }
    public int getL() {
        return l;
    }
    public void setL(int l) {
        this.l = l;
    }
    public int getW() {
        return w;
    }
    public void setW(int w) {
        this.w = w;
    }
    public int getFlag() {
        return flag;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
   
   
   
}
 
package geren_04;

import java.util.ArrayList;

public class KUtil 
{
    public static int max(int... a)//最大
    {
        int max=a[0];
        for(int i:a)
        {
            if(max<=i)
            {
                max=i;
            }
        }
        return max;
    }
    public static int min(int... a)//最小
    {
        int min=a[0];
        for(int i:a)
        {
            if(min>=i)
            {
                min=i;
            }
        }
        return min;
    }
    public static K KCreateAll(int w,int l,int[][] list)//初始化最大的块  k all,有bug刚改,所有怀疑下面几个也有bug
    {
        D zs=new D(0,0);
        D ys=new D(l,0);
        D zx=new D(0,w);
        D yx=new D(l,w);
        K k=new K(zs,ys,zx,yx);
        ArrayList<H> kList=new ArrayList<H>();
        ArrayList<D> hList=new ArrayList<D>();
        H h=new H();
        D d=new D();
        D z=new D();
        D y=new D();
        
        
        for(int i=0;i<w;i++)
        {
            hList=new ArrayList<D>();
            h=new H();
            z=new D();
            y=new D();
            
            z.setX(0);
            y.setX(l);
            z.setY(i);
            y.setY(i);
            for(int t=0;t<l;t++)
            {
                d=new D();
                d.setX(t);
                d.setY(i);
                d.setNum(list[i][t]);
                hList.add(d);
                //hList.get(hList.size()-1).show();
            }
            
            h.setZ(z);
            h.setY(y);
            h.setValue(hList);
            h.getSum();
            kList.add(h);
            kList.get(0).show();
        }
        k.setValue(kList);
        k.getSum();
        return k;
    }
    public static ArrayList<K> KCreate(int w,int l,int[][] list)//创建块,怀疑有bug
    {
        ArrayList<K> EKList=new ArrayList<K>();
        K k;
        D d=new D();
        H h;
        ArrayList<H> kList=new ArrayList<H>();
        ArrayList<D> hList=new ArrayList<D>();
        //创建块
        for(int i=0;i<w;i++)
        {
            for(int t=0;t<l;t++)
            {
                kList.clear();
                hList.clear();
                d.setX(t);
                d.setY(i);
                d.setNum(list[i][t]);
                k=new K(d,d,d,d);
                h=new H(d,d);
                hList.add(d);
                h.setValue(hList);
                kList.add(h);
                k.setValue(kList);
                k.getSum();
            }
        }
        return EKList;
    }
    public static K KAdd(K k1,K k2,K all)//块的加法,怀疑有bug
    {
        D zs=new D();
        D ys=new D();
        D zx=new D();
        D yx=new D();
        D d1=new D();
        D d2=new D();
        //确定四个角
        //左上的点
        d1=k1.getZS();
        d2=k2.getZS();
        //x
        zs.setX(min(d1.getX(),d2.getX()));//取最小
        //y
        zs.setY(min(d1.getY(),d2.getY()));//取最小
        //右上的点
        d1=k1.getYS();
        d2=k2.getYS();
        //x
        ys.setX(min(d1.getX(),d2.getX()));//取最小
        //y
        ys.setY(min(d1.getY(),d2.getY()));//取最小
        //左下的点
        d1=k1.getZX();
        d2=k2.getZX();
        //x
        zx.setX(min(d1.getX(),d2.getX()));//取最小
        //y
        zx.setY(min(d1.getY(),d2.getY()));//取最小
        //右下的点
        d1=k1.getYX();
        d2=k2.getYX();
        //x
        yx.setX(min(d1.getX(),d2.getX()));//取最小
        //y
        yx.setY(min(d1.getY(),d2.getY()));//取最小
        //
        K k=new K(zs,ys,zx,yx);
        /*赋值*/
        //获得LIST
        ArrayList<H> kList=new ArrayList<H>();
        ArrayList<D> hList=new ArrayList<D>();
        H h=new H();
        D d=new D();
        D z=new D();
        D y=new D();
        z.setX(zs.getX());
        y.setX(ys.getX());
        for(int i=0;i<k.getW();i++)
        {
            hList.clear();
            z.setY(zs.getY()+i);
            for(int t=0;t<k.getL();t++)
            {
                d=all.getD(t, i);
                hList.add(d);
            }
            h.setZ(z);
            h.setY(y);
            h.setValue(hList);
            h.getSum();
            kList.add(h);
        }
        k.setValue(kList);
        k.getSum();
        return k;
    }
    
}

  

 

 posted on 2019-03-23 17:11  Aurinko  阅读(123)  评论(0编辑  收藏  举报