环形二维数组求最大子矩阵

先上代码:

  1 //作者:王炳午、董龙洋。日期:2015.3.24.
  2 #include <iostream>
  3 #include<stdlib.h>
  4 #include<time.h>
  5 using namespace std;
  6 int maxMax(int amax[])     //求最大
  7 {
  8     int i;
  9     int sum = 0;
 10     int max;
 11     int max_max;
 12     max = 0;
 13     max_max = 0;
 14     for (i = 0; i < 9; i++)
 15     {
 16         max += amax[i];
 17         if (max < 0)
 18             max = 0;
 19         if (max != 0)
 20         {
 21             sum++;
 22             if (sum >= 5)
 23                 max = 0;
 24             if (max > max_max)
 25                 max_max = max;
 26         }
 27         else
 28         {
 29             sum = 0;
 30             if (sum >= 5)
 31                 max = 0;
 32             if (max > max_max)
 33                 max_max = max;
 34         }
 35     }
 36     if (max_max == 0)
 37     {
 38         for (int i = 0; i < 9; i++)
 39         {
 40             if (max_max == 0)
 41             {
 42                 max_max = amax[i];
 43             }
 44             else
 45             {
 46                 if (max_max < amax[i])
 47                 {
 48                     max_max = amax[i];
 49                 }
 50             }
 51         }
 52     }
 53     return max_max;
 54 }
 55 
 56 int main()
 57 {
 58     int a[3][5];
 59     int b[3][9];      //合并后的数组;
 60     int i;
 61     int j;
 62     int sum = 0;
 63     int overmax;
 64     int max;
 65     int max_max;
 66     int bmax[100];
 67     int amax[100];
 68     cout << "---------------------求数组中子数组和的最大值的小程序----------------------" << endl;
 69     cout << endl;
 70     cout << "得到的二维随机整数数组(3行5列)如下:" << endl;
 71     srand((unsigned)time(NULL));//随机数种子为当前计算机时间。
 72 
 73     for (i = 0; i < 3; i++)      //输入数组中的每个元素
 74         for (j = 0; j < 5; j++)
 75             a[i][j] = (rand() % 21 - 10);
 76     for (i = 0; i < 3; i++)
 77         for (j = 0; j < 9; j++)
 78         {
 79             if (j >= 0 && j <= 4)
 80                 b[i][j] = a[i][j];
 81             else
 82                 b[i][j] = a[i][j - 5];
 83         }
 84 
 85     for (i = 0; i < 3; i++)        //每行数据比较;
 86     {
 87         max = 0;
 88         max_max = 0;
 89         for (j = 0; j < 9; j++)
 90         {
 91             max += b[i][j];
 92             if (max < 0)
 93                 max = 0;
 94             /*if (max != 0)
 95             {
 96             sum = 0;
 97             sum++;
 98             }
 99             if (sum >= 5)
100             max = 0;
101             if (max > max_max)
102             max_max = max;*/
103             if (max != 0)
104             {
105                 sum++;
106                 if (sum >= 5)
107                     max = 0;
108                 if (max > max_max)
109                     max_max = max;
110             }
111             else
112             {
113                 sum = 0;
114                 if (sum >= 5)
115                     max = 0;
116                 if (max > max_max)
117                     max_max = max;
118             }
119         }
120         if (max_max == 0)
121         {
122             max_max = b[0][0];
123             for (j = 0; j < 9; j++)
124             {
125                 if (max_max < b[i][j])
126                     max_max = b[i][j];
127             }
128         }
129         bmax[i] = max_max;           //0到2
130     }
131     for (j = 0; j < 9; j++)        //上中组合两两组合保存在amax数组
132     {
133 
134         amax[j] = b[0][j] + b[1][j];
135 
136     }
137     bmax[3] = maxMax(amax);
138 
139     for (j = 0; j < 9; j++)        //中下组合两两组合保存在amax数组
140     {
141 
142         amax[j] = b[1][j] + b[2][j];
143 
144     }
145     bmax[4] = maxMax(amax);
146 
147     for (j = 0; j < 9; j++)        //上中下组合两两组合保存在amax数组
148     {
149 
150         amax[j] = b[1][j] + b[2][j] + b[0][j];
151 
152     }
153     bmax[5] = maxMax(amax);
154 
155 
156 
157 
158 
159     for (i = 0; i < 3; i++)     //输出数组中每个元素
160         for (j = 0; j < 5; j++)
161         {
162             cout << a[i][j] << "	";
163             if ((j + 1) % 5 == 0)
164             {
165                 cout << endl;
166             }
167         }
168     //求二维数组子矩阵最大值。
169     overmax = bmax[0];
170     for (i = 0; i < 6; i++)
171     {
172         if (overmax < bmax[i])
173         {
174             overmax = bmax[i];
175         }
176     }
177     cout << "子矩阵和最大值为:" << overmax << endl;
178     /*    for(i=0;i<6;i++)
179     {
180     cout<<bmax[i]<<"	";
181     }*/
182     return 0;
183 }

结果:

本次试验有些仓促,两人的时间都有点少,本次我充当的是代码书写员,我和我的小伙伴轮流替换角色,都收获了很多,

觉得两人结对,会使发现错误的几率大大增加,特别是一些算法上的问题,对代码的完成速度和质量都有很大的好处。

以下是我和小伙伴的合照(左边是我,右边是我的小伙伴)

原文地址:https://www.cnblogs.com/mtant/p/4395644.html