zoukankan      html  css  js  c++  java
  • 二维数组

    思路:

    由已知一维数组的算法,用一个新的二维数组对该二维区域的数组进行求和,例如新的二维数组的第5个位置,就代表从1到5斜对角线的块状区域的和,即1,2,4,5这4个数的和,x个位置表示从1到x的斜对角块状区域的和,利用循环一一求出对应的和,一次循环即可,这个循环复杂度为O(nm)

     1 package shuzu;
     2 import java.io.IOException;
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.List;
     6 public classs shuzu2 {
     7         public static void main(String[] args) throws IOException {
     8             Integer c[][]= {
     9                 {1,2,-3,-1,2,2},
    10                 {-3,4,5,1,-1,3},
    11                 {-2,-3,4,1,4,3}
    12             };
    13             //求和
    14             List<List<Integer>> main=new ArrayList<List<Integer>>();
    15             for(int i=0;i<c.length;i++)
    16             {
    17                 List<Integer> heng=new ArrayList<Integer>();
    18                 for(int j=0;j<c[0].length;j++)
    19                 {
    20                     if(j!=0)
    21                         heng.add(c[i][j]+heng.get(j-1));
    22                     else heng.add(c[i][j]);
    23                 }
    24                 if(i!=0)
    25                     main.add(addList(heng,main.get(i-1)));
    26                 else main.add(heng);
    27             }
    28             //求最大值
    29             
    30             int max=main.get(0).get(0);
    31             for(int z=0;z<main.size();z++)
    32             {
    33                 int temp=Collections.max(main.get(z));
    34                 if(max<temp)
    35                 {
    36                     max=temp;
    37                 }
    38             }
    39             //确定一位置
    40             for(int i1=0;i1<main.size();i1++)
    41             {
    42                 for(int j1=0;j1<main.get(0).size();j1++)
    43                 {
    44                     //确定二位置
    45                     for(int i2=i1+1;i2<main.size();i2++)
    46                     {
    47                         for(int j2=j1+1;j2<main.get(0).size();j2++)
    48                         {
    49                             int g_max=0;
    50                             if(i1!=0&&j1!=0)
    51                             {
    52                                 g_max=(main.get(i2).get(j2)+main.get(i1-1).get(j1-1)-main.get(i2).get(j1-1)-main.get(i1-1).get(j2));
    53                                 
    54                             }
    55                             else if(i1!=0)
    56                             {
    57                                 g_max=(main.get(i2).get(j2)-main.get(i1-1).get(j2));
    58                             }
    59                             else if(j1!=0)
    60                             {
    61                                 g_max=(main.get(i2).get(j2)-main.get(i2).get(j1-1));
    62                             }
    63                             if(max<g_max)
    64                             {
    65                                 max=g_max;
    66                             }
    67                         }
    68                     }
    69                 }
    70             }
    71             System.out.println("该二维数组整理区域和为:"+main);
    72             System.out.println("该二维数组最大块区域和为:"+max);
    73         }
    74         public static List<Integer> addList(List<Integer> a,List<Integer> b)
    75         {
    76             List<Integer> sum=new ArrayList<Integer>();
    77             for(int i=0;i<a.size();i++)
    78             {
    79                 sum.add(a.get(i)+b.get(i));
    80             }
    81             return sum;
    82         }
    83 }
  • 相关阅读:
    HDU 5640 King's Cake
    HDU 5615 Jam's math problem
    HDU 5610 Baby Ming and Weight lifting
    WHU1604 Play Apple 简单博弈
    HDU 1551 Cable master 二分
    CodeForces659C Tanya and Toys map
    Codeforces 960E 树dp
    gym 101485E 二分匹配
    Codeforces 961E 树状数组,思维
    Codeforces Round #473 (Div. 2) D 数学,贪心 F 线性基,模板
  • 原文地址:https://www.cnblogs.com/mawangwang/p/11071506.html
Copyright © 2011-2022 走看看