• 二维数组求最大子数组


     结对开发:张生辉,康治家
    1 package 二维数组;
     2 import java.util.*;
     3 public class Tarray {
     4     static Scanner scanner = new Scanner(System.in);
     5     public static void main(String args[]){
     6         int m,n;
     7         int b;
     8         Scanner scanner = new Scanner(System.in);
     9         
    10         System.out.println("二维数组的列数:");
    11         m = scanner.nextInt();
    12         System.out.println("二维数组的行数:");
    13         n = scanner.nextInt();
    14         int arr[][] = new int[n][m];
    15          System.out.println("请输入二位数组:");
    16         for(int i = 0;i<n;i++)
    17         for(int j=0;j<m;j++)
    18         {
    19             arr[i][j] = scanner.nextInt();
    20         }
    21         System.out.println("
    ");
    22         b = maxArrSum(arr);
    23         System.out.println("最大联通数组的最大和为"+b);
    24     }
    25     
    26 
    27     public static int[][] arrSum(int arr[][]){
    28         int m = arr.length;
    29         int n = arr[0].length;
    30         int p[][] = new int[m+1][n+1];
    31         p[0][0] = arr[0][0];
    32         for(int i=0; i<=m; i++) p[i][0] = 0;
    33         for(int i=0; i<=n; i++) p[0][i] = 0;
    34         for(int i=1; i<=m; i++){
    35             for(int j=1; j<=n; j++){
    36                 p[i][j] = p[i-1][j] + p[i][j-1] + arr[i-1][j-1] - p[i-1][j-1];
    37             }
    38         }
    39         return p;
    40     }
    41 
    42     //遍历所有二维数组的矩形区域
    43     
    44     static int  maxArrSum(int arr[][]){
    45         int m = arr.length;
    46         int n = arr[0].length;
    47         int p[][] = arrSum(arr);
    48         int ans = Integer.MIN_VALUE;
    49         for(int i=1; i<=m; i++){
    50             for(int j=1; j<=n; j++){
    51                 for(int endi=i; endi <=m; endi++){
    52                 for(int endj=j; endj<=n; endj++){    
    53                         int sum = p[endi][endj] - p[i-1][endj] - p[endi][j-1] + p[i-1][j-1];
    54                         if(ans < sum) ans = sum;    
    55                     }
    56                 }    
    57             }    
    58         }    
    59         return ans;    
    60     }
    61     
    62     
    63 
    64 }

    两人合作过程:

    首先两人商量思想,最后总结出一套可行的思路,之后一人负责编程,一个负责对代码进行分析

    体会:

    总体感觉良好吗,但是效率比独自一人的时候低

    如何解决冲突:

    和平谈判,协商,总结看看谁的更加合适



  • 相关阅读:
    18寒假第一测
    P1932 A+B A-B A*B A/B A%B Problem
    POJ 2823 Sliding Window
    堆排序
    [LeetCode] Candy
    [LeetCode] Single Number II
    [LeetCode] Single Number
    [LeetCode] Binary Tree Postorder Traversal
    [LeetCode] Binary Tree Preorder Traversal
    [LeetCode] Insertion Sort List
  • 原文地址:https://www.cnblogs.com/shenghuizhang/p/6679733.html
Copyright © 2020-2023  润新知