数组2

课堂作业八

返回一个整数数组中最大子数组的和2 要求:

输入一个整形数组,数组里有正数也有负数。

数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。

如果数组A[0]……A[j-1]首尾相邻,允许A[i-1],…… A[n-1]A[0]……A[j-1]之和最大。

同时返回最大子数组的位置。

求所有子数组的和的最大值。

结对编程要求:

两人结对完成编程任务。

一人主要负责程序分析,代码编程。

一人负责代码复审和代码测试计划。

发表一篇博客文章讲述两人合作中的过程、体会以及如何解决冲突(附结对开发的工作照)

一.设计思想

  虽然在课堂上听了老师的解释有些想法,先从最大的数字开始分别找第二大,第三大,直到找到最小的正数,然后再判断连通性即可,如果连通两个数相加,如果两个数不连通再和其他的比较,然后在以第二大开始是否有连通的。。依次进行,但是在实际编程过程之后遇到了不少的问题,无法继续进行,我和我的小伙伴目前还没攻克问题,还需要时间,向别人请教

代码:

public class MaxSubArray {

 

    public MaxSubArray() throws Exception {

        // TODO Auto-generated constructor stub

       

    }

 

    public static void main(String[] args) throws Exception {

        // TODO Auto-generated method stub

        int array[][] = inputArray();

        for(int i = 0; i < array.length;i++)

        {

            for(int j = 0; j < array[i].length; j++)

                System.out.printf("%6d",array[i][j]);

            System.out.println();

        }

       

        System.out.println("*************************************************");

        int sum = 0;

        for(int i = 0; i < array.length;i ++)

        {

            for(int j = 0; j < array[i].length; j++)

            {

                if(array[i][j]>0&&tag[i][j]==0)

                {

                    int temp = branch(array,i,j);System.out.println(" "+temp);

                    if(sum<temp)

                        sum = temp;

                }

            }

        }

        System.out.println("最大子数组之和为:"+sum);

 

    }

    //the method is to input the array from file.

    public static int[][] inputArray() throws Exception

    {

        File f = new File("C:/Users/acer-pc/desktop/input.txt");

        if(!f.exists())

        {

            System.out.println("文件不存在!");

            return null;

        }

        BufferedReader read = new BufferedReader(new FileReader(f));

        String rowstr = read.readLine();

        String colstr = read.readLine();

       

        int row = Integer.parseInt(rowstr);

        int col = Integer.parseInt(colstr);

       

        int [][] array = new int[row][col];

        tag = new int[row][col];

        for(int i = 0; i < row; i++)

        {

            rowstr = read.readLine();

            String[] colmun = rowstr.split("[,]");

            for(int j = 0; j < col; j++)

            {

                int col_ = Integer.parseInt(colmun[j]);

                array[i][j]= col_;

                tag[i][j] = 0;

            }

        }

        read.close();

        return array;

    }

   

    /**

     * the next method expected to calculate the max sum of each positive number.

     * and the element which added should include zero.

     */

    public static int getMaxSum(int[][] array)

    {

        int rows = array.length;

        int cols = array[0].length;

        int sum = 0;

        for(int i = 0; i < rows; i++)

        {

            for(int j = 0; j < cols; j++)

            {

                if(array[i][j]>=0)

                    sum+=array[i][j];

            }

        }

        return sum;

    }

   

    /**

     *

     */

    public static int findWeight(int[][] array,int index_x,int index_y,int m,int n)

    {

        int rows = array.length;

        int cols = array[0].length;

        int minimun=0,result = -1999999999;

        int[] x = {index_x-1,index_x,index_x+1,index_x};

        int[] y = {index_y,index_y+1,index_y,index_y-1};

        for(int index = 0; index < 4; index++)

        {

            int i = x[index],j = y[index];

            if(i<0||i>=rows||j<0||j>=cols)

                continue;

            if(i==m&&j==n)

                minimun = -1999999999;

            else if(array[i][j] >= 0)

                minimun = 0;//array[i][j];

            else{

                minimun =array[i][j]+findWeight(array,i,j,index_x,index_y);

            }

            if(result < minimun)

                result = minimun;

        }

        return result;

    }

   

    /**

     * 以指定元素求该元素所在整数块整数

     * @param array 原来的二维数组

     * @param cur_x 当前元素第一个下标

     * @param cur_y 第二个下标

     * @return

     */

    public static int branch(int[][] array,int cur_x,int cur_y)

    {

        int rows = array.length;

        int cols = array[0].length;

       

        int[] x = {cur_x-1,cur_x,cur_x+1,cur_x};

        int[] y = {cur_y,cur_y+1,cur_y,cur_y-1};

        int sum = array[cur_x][cur_y];System.out.print(array[cur_x][cur_y]+" ");

        tag[cur_x][cur_y] = 1;

        for(int index = 0; index < 4; index++)

        {

            int i = x[index],j = y[index];

            if(i<0||i>=rows||j<0||j>=cols)

                continue;

            if(array[i][j]>=0&&tag[i][j]==0)

            {

                sum += branch(array,i,j);

            }

        }

       

        return sum;

    }

    private static int[][] tag;

}

原文地址:https://www.cnblogs.com/ever1961211/p/6994518.html