zoukankan      html  css  js  c++  java
  • 二维数组最大子数组和

    一.实验题目

    求一个二维数组中和最大的子数组。

    二.实验思路

    基于我们第一次合作时求的一位数组最大子数组,加上一层循环来遍历二维数组中的所有子矩阵的情况。

    第一步:先利用上次的方法求每一行的情况,将每行结果存入一个一位数组中,构成一个二维数组A[][]。

    第二步:将上述二维数组每一列利用求每一行的方法再次遍历。将每个结果与A[][]中的值比较取大值。

    第三步:输出这个最大值。

    三.代码

      1 // ketang5.cpp : 定义控制台应用程序的入口点。
      2 //人员:   张世通 梁世豪
      3 
      4 #include "stdafx.h"
      5 #include "iostream"
      6 #include "fstream"
      7 using namespace std;
      8 
      9 /*二维数组压缩函数*/
     10 void yasuo(int **Source,int **Destion,int row,int line)
     11 {//将每一行的数组元素都用枚举法求出子数组
     12     int i,j,add,count;
     13     for(i=0;i<row;i++)
     14     {
     15         count=0;
     16         for(j=0;j<line;j++)
     17         {
     18             add=0;
     19             for(int l=0;l<line-j;l++)                            //每一行按顺序进行枚举存入数组中
     20             {
     21                 add=add+Source[i][j+l];
     22                 Destion[i][count+l]=add;
     23             }
     24             count=count+line-j;
     25         }
     26     }
     27 }
     28 
     29 /*求最大值函数*/
     30 int Max(int **Destion,int row,int line)
     31 {
     32     int i,j,add,max;
     33     max=Destion[0][0];                                                //将数组的第一个数赋值给max
     34     for(j=0;j<line;j++)
     35     {
     36         for(i=0;i<row;i++)
     37         {
     38             add=0;
     39             for(int r=0;r<row-i;r++)
     40             {
     41                 add=add+Destion[r+i][j];                            //逐列对数组进行枚举,求最大值
     42                 if(max<add)
     43                 {
     44                     max=add;
     45                 }
     46             }
     47         }
     48     }
     49     return max;
     50 }
     51 
     52 /*数组初始化*/
     53 void chushihua(int**Arr,int row,int line)
     54 {
     55     cout<<"请输入数组数据"<<endl;
     56     for(int i=0;i<row;i++)
     57     {
     58         for(int j=0;j<line;j++)
     59         {
     60             cin>>Arr[i][j];
     61         }
     62     }
     63 }
     64 
     65 /*释放空间*/
     66 void Delete(int **Arr,int row)
     67 {
     68     for(int i=0;i<row;i++)
     69     {
     70         delete[]Arr[i];
     71     }
     72     delete[]Arr;
     73 }
     74 int main()
     75 {
     76     char s;
     77     int row,col1,col2;                                //定义行数列数
     78     cout<<"请输入行数:";
     79     cin>>row;
     80     cout<<"请输入列数:";
     81     cin>>col1;    
     82     int **A;                                        //动态定义二维数组
     83     int **B;
     84     col2=col1*(col1+1)/2;
     85     A=new int*[row];
     86     for(int k=0;k<row;k++)
     87     {
     88         A[k]=new int[col1];
     89     }
     90     B=new int*[row];
     91     for(int l=0;l<row;l++)
     92     {
     93         B[l]=new int[col2];
     94     }
     95     chushihua(A,row,col1);
     96     yasuo(A,B,row,col1);
     97     int max=Max(B,row,col2);
     98     cout<<"最大子数组之和为:"<<max<<endl;
     99     cout<<endl;
    100     cout<<"是否继续(Y/N)";
    101     cin>>s;
    102     while(s!='Y'&&s!='y'&&s!='N'&&s!='n')
    103     {
    104         cout<<"输入错误,请重新输入(Y/N)";
    105         cin>>s;
    106     }
    107     if(s=='Y'||s=='y')
    108     {
    109         main();
    110     }
    111     else                                            //释放数组空间
    112     {
    113         Delete(A,row);
    114         Delete(B,row);
    115     }
    116     return 0;
    117 }

    四.运行截图

     

    五.收获体会

    第一点,这次结对开发的过程让我学会了利用已有资源进行程序的开发,正是有了上次一位数组求最大子数组的基础,这次求二维数组最大子数组才有了正确的思路。

     

    第二点,这是和世通的第二次结对开发程序,他的思路很清晰,编程能力也很好,有很多值得我学习的地方。以后应该还有很多合作的机会,我都会珍惜的。

     

    第三点,我觉得结对开发这个主意应该早点应用到我们的学习中,这种方法下我们会尽力去思考问题的解决方法,基础差一点的在另一方的影响下也会学到不少实用的东西。而不是遇见不会的就百度一个程序去应付老师而造成恶性循环,给自己扣上一个“不会编程”的帽子来逃避。

    六.合影

  • 相关阅读:
    hdu 3790 最短路径问题
    hdu 2112 HDU Today
    最短路问题 以hdu1874为例
    hdu 1690 Bus System Floyd
    hdu 2066 一个人的旅行
    hdu 2680 Choose the best route
    hdu 1596 find the safest road
    hdu 1869 六度分离
    hdu 3339 In Action
    序列化和反序列化
  • 原文地址:https://www.cnblogs.com/zglsh/p/4369598.html
Copyright © 2011-2022 走看看