二维数组中求其中和最大的子矩阵(结对开发)


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

感想:两人一起去构思一起去想算法,本题最难的是算法的构思,借鉴曹同学的算法我们在原来的一些基础上,共同完成了本次任务(充当敲代码的是我,上回是我是领航员),我觉得两人结对,会使发现错误的几率大大增加,特别是一些算法上的问题,对代码的完成速度和质量都有很大的好处。以下是我和小伙伴的合照(左边是我,右边是我的小伙伴)

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