zoukankan      html  css  js  c++  java
  • 最大联通子数组求和

    设计题目:求一个二维数组的连通的数组中和最大的最大值。

    设计思路:

    先建立二维数组并遍历二维数组,将所有的正整数进行分块,然后验证是否联通,如果不联通,则判断路径。

    代码:

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

      截图:

    总结:这次实验是我和张家星同学一块努力完成的,其间在弄实现的算法时较为麻烦,但在我们共同努力下通过上几次实验基础还完成了这次实验,对复杂的问题进行简单化,逐步完成。

  • 相关阅读:
    ELF 格式简介
    gdb 使用说明;ARM 汇编
    博士研究生的组会PPT汇报相关-labgirls
    浅尝辄止·认识Blazor及基础使用
    VBA·Function的基础使用
    WCF·无法自动进入并单步执行服务器。调试器未能在服务器进程中停止。
    Word·查找任意汉字的方法
    VBA·编译错误:ByRef参数类型不符
    排坑·QQ浏览器打开MD文件时显示下载不能直接打开
    MSSQL·PIVOT关键字实现列转行
  • 原文地址:https://www.cnblogs.com/zyx111/p/6680502.html
Copyright © 2011-2022 走看看