求数组中最大子数组的和03

  题目:返回一个二维整数数组中的最大连通子数组的和

  要求:输入一个二维整形数组,有正数、有负数;

       求所有子数组的和的最大值。

  设计思路:刚看到这个题目时,说实话,确实一点儿思路都没有,后来听同学去讲台上讲思路,经过课下结合之前学过的知识和队友讨论了讨论,捋清了基本思路,大致如下:利用遍历的思想,以及寻找路径的方法,在遍历二维数组中按照优化思想逐个找出理想的数值相加,按照最小代价直到选出二维数组中最大的子数组的和为止。

  源代码:

  1 //结对开发成员;张晓菲 张哲
  2 //本次实验题目为:求二维数组中的最大连通子数组的和
  3 #include<iostream>
  4 #include<ctime>
  5 #include<fstream>
  6 using namespace std;
  7 #define N 100
  8 
  9 typedef struct
 10 {
 11     int array[N];
 12     int col[N][N];
 13     int countnum;
 14 }Struct;//定义结构体变量
 15 
 16 void input(Struct &num, int x, int y)
 17 {//input用于实现从文件中读取行和列并输出到屏幕上
 18     num.countnum = x*y;
 19     int i = 1;
 20     int a, b;
 21     ifstream in = ifstream("input.txt");
 22     in >> a;
 23     in >> b;
 24     num.countnum = a*b;
 25     while (in >> num.array[i])
 26     {//将in文件中的数字读取到数组中
 27         ++i;
 28     }
 29     in.close();//读取完毕关闭文件in
 30     for (int i = 1; i <= num.countnum; i++)
 31     {
 32         cout << num.array[i] << " ";
 33         if (i%b == 0)
 34         {
 35             cout << endl;
 36         }
 37     }//输出文件导入的数组到屏幕
 38     for (int i = 1; i <= num.countnum; i += y)
 39     {
 40         for (int j = i; j <= i + y - 2; j++)
 41         {
 42             num.col[j][j + 1] = 1;
 43             num.col[j + 1][j] = 1;
 44         }
 45     }
 46     for (int i = 1 + y; i<num.countnum; i += y)
 47     {
 48         for (int j = i; j <= i + x - 1; j++)
 49         {
 50             num.col[j][j - y] = 1;
 51             num.col[j - y][j] = 1;
 52         }
 53     }
 54 }
 55 
 56 void traverse(Struct &num, int v, int visit[], int &b, int &max, int x)
 57 {//通过对数组的遍历寻找最大连通子数组
 58     int a = 0, var = 0;
 59     visit[v] = 1;
 60     max += num.array[v];
 61     if (max >= b)
 62     {
 63         b = max;
 64     }
 65    for (int w = 1; w <= num.countnum; w++)
 66     {
 67         for (int c = 1; c <= num.countnum; c++)
 68         {
 69             if ((visit[w] == 0) && (num.col[c][w] == 1) && (visit[c] == 1))
 70             {
 71                 a = w; 
 72                 var = 1; 
 73                 break;
 74             }
 75         }
 76         if (var == 1)
 77             break;
 78     }
 79     for (int w = 1; w <= num.countnum; w++)
 80     {
 81         for (int c = 1; c <= num.countnum; c++)
 82         {
 83             if ((visit[w] == 0) && (num.col[c][w] == 1) && (visit[c] == 1))
 84             {
 85                 if (num.array[a]<num.array[w])
 86                     a = w;
 87             }
 88         }
 89     }
 90     if (b + num.array[a]<0)
 91     {
 92         num.col[v][a] = 0;
 93     }
 94     else
 95         traverse(num, a, visit, b, max, x);
 96 }
 97 
 98 
 99 int main()
100 {
101     int x, y;
102     fstream fs("input.txt");
103     fs >> x;
104     fs >> y;
105     cout << x << " " << y << endl;
106     Struct num;
107     input(num, x, y);
108     int v = 1, b[N] = { 0 }, h = 0;
109     for (int i = 1; i <= num.countnum; i++)
110     {
111         if (num.array[i]<0)
112         {
113             b[i] = num.array[i];
114         }
115         else
116         {
117             int visit[N] = { 0 };
118             int max = 0;
119             traverse(num, i, visit, b[i], max, x);
120         }
121     }
122 
123     int max = b[1];
124     for (int i = 2; i <= num.countnum; i++)
125     {
126         if (b[i]>max)
127             max = b[i];
128     }
129     cout << "该数组中最大连通子数组的和为:" << max << endl;
130 }
View Code

运行结果截图:

时间计划日志:

  本打算每天花两个小时的时间把最大连通子数组的和这个程序写出来,实际平均每天用的时间要比这个多一点点。

缺陷记录日志:

  没有判断文件中如果是非整数或者非数字该如何处理。

时间记录日志:

日期 开始时间 结束时间 中断时间(min) 净时间(min) 活动 备注

3月28号

星期一

14:00 15:50 10(课间) 100 上课 软件工程

3月29号

星期二

20:00 22:00 0 120 编程 四则运算网页版

3月30号

星期三

15:00 17:00 10(休息) 110 编程 四则运算网页版
20:00 22:10 10 120 编程 四则运算网页版

3月31号

星期四

14:00 15:50 10(课间) 100 上课 软件工程
  20:30 22:00 10(休息) 80 编程 最大连通子数组和

4月2号

星期六

14:00 17:00 20(休息) 160 编程 四则运算网页版

4月3号

星期日

14:30 18:00 30 180 编程

四则运算网页版

 4月4号

星期一

19:30 22:30 0 180 编程

四则运算网页版

 4月5号

星期二

8:00 10:00 0 120 编程

 最大连通子数组和

19:30 23:00 0 210 编程

四则运算网页版

 4月6号

星期三

14:20 17:10 0 150 编程

 最大连通子数组和

最大连通子数组和

整理、发博客

原文地址:https://www.cnblogs.com/quite-love/p/5360185.html