循环二维和最大子数组

1.设计思路

结合循环的一维子数组与二维子数组,因为我的二维最大子数组的和计算时,将二维的数组最大和转化成了一维的数组;因此,将这两个思路结合,就可以轻松的写出二维的和最大子数组。

2.源代码

#include<iostream>

#include<ctime>

using namespace std;

 

int main()

{

         int m, n;                      //数组大小

         int i, j, p, q, o;                //循环变量

         int a[100][100];               //数组

         int b[100][200];               //代替循环矩阵

         int sh, x;                     //数值范围

 

         int sum, sum1 = -100, sum2 = -100;     //计算和

         int max = a[0][0];             //最大值

         int s[200];                    //转化成的一位数组

         int k = 0;                     //控制行数的变量

 

         cout << "请输入矩阵的行列数(上限100):";

         cin >> m >> n;

         cout << "请输入数值范围(a-b):";

         cin >> sh >> x;

 

         cout << "原矩阵为:" << endl;

 

         srand((unsigned)time(NULL));

         for (i = 0; i<m; i++)

         {

                   for (j = 0; j<n; j++)

                   {

                            a[i][j] = rand() % (x - sh + 1) + sh;

                            cout << a[i][j] << "  ";

                   }

                   cout << endl;

         }

 

         for (i = 0; i < m; i++)

         {

                   for (j = 0; j < 2 * n; j++)

                   {

                            int d = j;

                            if (d>=n)

                            {

                                     d = d - n;

                            }

                            b[i][j] = a[i][d];

                   }

         }

 

         for (j = 0; j < 2 * n; j++)

         {

                   s[j] = 0;

         }

         for (i = 0; i<m; i++)

         {

                   while (k + i<m)

                   {

                            for (j = 0; j < 2 * n; j++)

                            {

                                     s[j] = s[j] + b[k + i][j];

                            }

 

                            for (int l = 0; l < n; l++)

                            {

                                     for (p = l; p < n + l; p++)

                                     {

                                               for (q = l; q <= p + l; q++)

                                               {

                                                        sum = s[q + l];

                                                        for (o = q + l + 1; o <= p; o++)

                                                                 sum = sum + s[o];

 

                                                        if (sum > sum1)

                                                        {

                                                                 sum1 = sum;

                                                        }

                                               }

                                               if (sum1 > sum2)

                                               {

                                                        sum2 = sum1;

                                               }

                                     }

                            }

                            if (sum2 > max)

                            {

                                     max = sum2;

                            }

 

                            k++;

                   }

                   k = 0;

                   for (j = 0; j < 2 * n; j++)

                   {

                            s[j] = 0;

                   }

         }

 

         cout << "和最大子矩阵值为" << max << endl;

 

         return 0;

}

3.截图

4.总结

这一次的小程序也是在原有的代码上进行修改,而且是完全借用了其中的一段代码,修改是非常简单的;更进一步的说明了设计思路的重要,也说明了注释的重要,可以队友很快的看明白自己的程序,更有助于团队的合作,提高工作的效率。

posted on 2015-04-23 20:34  温柔的心  阅读(161)  评论(0编辑  收藏  举报

导航