1 #include <iostream> 2 #include<stdlib.h> 3 #include<time.h> 4 using namespace std; 5 6 int maxMax( int amax[]) //求最大 7 { 8 int i,j; 9 int max; 10 int max_max; 11 max = 0; 12 max_max = 0; 13 14 for (i = 0; i < 5; i++) 15 { 16 17 max += amax[i]; 18 if (max < 0) 19 max = 0; 20 if (max > max_max) 21 max_max = max; 22 } 23 if (max_max == 0) 24 { 25 max_max = amax[i]; 26 for (j = 0; j < 5; j++) 27 { 28 if (max_max < amax[i]) 29 max_max = amax[i]; 30 } 31 } 32 return max_max; 33 } 34 35 int main() 36 { 37 int a[3][5]; 38 int i; 39 int j; 40 int overmax; 41 int max = 0; 42 int max_max = 0; 43 int bmax[100]; 44 int amax[10]; 45 46 47 srand((unsigned)time(NULL));//随机数种子为当前计算机时间。 48 49 for (i = 0; i < 3; i++) //输入数组中的每个元素 50 for (j = 0; j < 5; j++) 51 a[i][j] = rand() % 21 - 10; 52 53 for (i = 0; i < 3; i++) //每行数据比较; 54 { 55 for (j = 0; j < 5; j++) 56 { 57 max += a[i][j]; 58 if (max < 0) 59 max = 0; 60 if (max > max_max) 61 max_max = max; 62 } 63 if (max_max == 0) 64 { 65 max_max = a[0][0]; 66 for (j = 0; j < 5; j++) 67 { 68 if (max_max < a[i][j]) 69 max_max = a[i][j]; 70 } 71 } 72 bmax[i] = max_max; //0到2 73 } 74 for (j = 0; j < 5; j++) //上中组合两两组合保存在amax数组 75 { 76 77 amax[j] = a[0][j] + a[1][j]; 78 79 } 80 maxMax(amax); 81 bmax[3] = max_max; 82 83 for (j = 0; j < 5; j++) //中下组合两两组合保存在amax数组 84 { 85 86 amax[j] = a[1][j] + a[2][j]; 87 88 } 89 maxMax(amax); 90 bmax[4] = max_max; 91 92 for (j = 0; j < 5; j++) //上中下组合两两组合保存在amax数组 93 { 94 95 amax[j] = a[1][j] + a[2][j] +a[0][j]; 96 97 } 98 maxMax(amax); 99 bmax[5] = max_max; 100 101 102 103 104 105 for (i = 0; i < 3; i++) //输出数组中每个元素 106 for (j = 0; j < 5; j++) 107 { 108 cout << a[i][j] << "\t"; 109 if ((j + 1) % 5 == 0) 110 { 111 cout << endl; 112 } 113 } 114 for (i = 0; i < 6; i++) 115 { 116 overmax = bmax[0]; 117 if (overmax < bmax[i]) 118 overmax = bmax[i]; 119 120 } 121 cout << overmax <<endl; 122 123 124 125 return 0; 126 }
感想:两人一起去构思一起去想算法,本题最难的是算法的构思,借鉴曹同学的算法我们在原来的一些基础上,共同完成了本次任务(充当敲代码的是我,上回是我是领航员),我觉得两人结对,会使发现错误的几率大大增加,特别是一些算法上的问题,对代码的完成速度和质量都有很大的好处。以下是我和小伙伴的合照(左边是我,右边是我的小伙伴)
原文:http://www.cnblogs.com/mtant/p/4357504.html