zoukankan      html  css  js  c++  java
  • 结对开发Ⅱ—利用文本求二维数组最大的子数组的和

    一、设计思路

    1、利用文件的读取流将文本中的数组都读取到定义的数组中;

    2、在求出任意一个子数组的和,然后比较最大值,并且将其输出;

    文本中的内容如下:

    二、源代码

      1 import java.util.*;
      2 import java.io.*;
      3 public class SuperMax2
      4 {
      5     public static void ShowArr(int arr[][],int line1,int line2,int row1,int row2)
      6      {
      7          for(int i=line1;i<=row1;i++)
      8          {
      9              for(int j=line2;j<=row2;j++)
     10              {
     11                  System.out.print(+arr[i][j]+" ");
     12              }
     13              System.out.println();  
     14          }
     15      }
     16     public static int GetSum(int arr[][],int n,int m,int k,int l)
     17      {
     18         int sum=0;
     19         for(int f=n;f<=k;f++)
     20          {
     21            for(int h=m;h<=l;h++)
     22             {
     23             sum+=arr[f][h];
     24             }
     25          }
     26         return sum;
     27      }
     28     public static void main(String[] args) throws NumberFormatException, IOException
     29     {
     30         int y=0;
     31         int x=0;
     32         String line;//一行数据
     33         
     34         int row1=0;
     35         File file=new File("input.txt");//存放数组数据的文件
     36         BufferedReader in=new BufferedReader(new FileReader(file));
     37         
     38         int[][] arr1 = new int[5][20];//读取出的数组
     39         int[][] arr = new int[5][20];//读取出的数组
     40         while((line=in.readLine())!=null)
     41         {
     42           String[] temp=line.split(",");
     43           for(int i=0;i<temp.length;i++)
     44            {
     45              arr1[row1][i]=(int) Integer.parseInt(temp[i]);
     46            }
     47            row1++;           
     48         }
     49          in.close();
     50          x=arr1[0][0];  //数组的行数
     51          y=arr1[1][0];  //数组的列数
     52       
     53         for(int i=0;i<x;i++)
     54         {
     55             for(int j=0;j<y;j++)
     56             {
     57                 arr[i][j] = arr1[i+2][j];
     58             }
     59         }
     60         System.out.println("文本中数组为:");
     61         for(int i=0;i<x ;i++ )//将数组显示出来
     62         {
     63             for(int j=0;j<arr.length-1 ;j++ )
     64             {
     65                 System.out.print(arr[i][j]+" ");
     66             }
     67             System.out.println();
     68         }
     69         int sum=0;
     70         int n;
     71         int m;
     72         int k;
     73         int l;
     74         Scanner sc=new Scanner(System.in);
     75         int line1=0;
     76         int line2=0;
     77         int row2=0;
     78         int row3=0;
     79         int max=arr[0][0];
     80         for(n=0;n<x;n++)
     81         {    
     82            for(m=0;m<y;m++)
     83             {
     84                 for(k=n;k<x;k++)
     85                  {
     86                     for(l=m;l<y;l++)
     87                     {
     88                         sum=GetSum(arr,n,m,k,l); 
     89                         if(sum>max)
     90                         {
     91                              max=sum;
     92                              line1=n;                //保存第一个数的行
     93                              line2=k;                //保存第二个数的行
     94                              row2=m;                    //保存第一个数的列
     95                              row3=l;                    //保存第二个数的列
     96                         }
     97                     }
     98                  }
     99               }
    100          }
    101         System.out.println("最大子数组为:");
    102         ShowArr(arr,line1,row2,line2,row3);
    103         System.out.println("最大子数组的和为:"+max);
    104     }
    105 }

    三、运行结果

    全负数

    全正数

    有正有负

    四、心得体会
       在这次结对开发的过程中遇到了好多问题,首先是在解决文件的读取的时候,一开始时使用的是read方法,但是
    发现这个是行不通的,因为没有考虑逗号的问题,和返回值得类型,read读出来不是整形的,所以最后尝试使用缓冲
    区的方法,,每次读取一行,并且用split方法用逗号将数组分割开,并且将该数组存到指定数组,这样就成功的转换
    了,然后通过遍历每一个字数组,求和,并且求出最大值。这次我们两人使用的时分工合作,最后进行代码复审。
    终于实现了该功能。

    五、有图为证

  • 相关阅读:
    弱口令扫描.python脚本
    扫描web目录的Python小脚本
    Linux /etc目录重要文件
    linux(centos7)系统优化
    泛型程序设计
    对象包装器与自动装箱
    打包apk时,提示:error: Error: Resource is not public.
    SpringMVC分页实现
    IDEA搭建基于maven 的SSM框架
    ssm框架搭建
  • 原文地址:https://www.cnblogs.com/KevinBin/p/4369834.html
Copyright © 2011-2022 走看看