Experience the fun of pair Development 2 (the Child array with the greatest sum of the two arrays) and the pair Array
I. Team members:
Xin 1-2 -2 class Gao Yang, Xin 1-1 -1 class Han xuedong
Ii. Question requirements
This time makes it more difficult than the previous one. It is changed to the Child array with the greatest sum of two-dimensional arrays.
Iii. Design Ideas
The last Implementation of an array, and then want to convert the two-dimensional array into a one-dimensional array, first solve the problem of the largest sub-array in the first line, the method is the same as the last time, then the second row is added to the first row and the second row to find the two rows, and store them in the three one-dimensional arrays respectively. Then, the maximum value is obtained for the three number groups.
Iv. Source Code
1 // erweishuzuqiuzuida. cpp: Defines the entry point for the console application. 2 // letter 1-2 -2 class Gao Yang, letter 1-1 -1 class Han xuedong 3 4 # include "stdafx. h "5 # include" fstream. h "6 # include" iostream. h "7 # include" stdio. h "8 9 # define MAXSIZE 50 10 11 12 void read (int array [] [MAXSIZE], int & len1, int & len2) // read File Information, and write the array 13 {14 ifstream infile ("array.txt"); 15 if (! Infile) 16 cout <"failed to read! "<Endl; 17 else 18 {19 infile> len1> len2; 20 for (int I = 0; I <len1; I ++) 21 {22 for (int j = 0; j <len2; j ++) 23 {24 infile> array [I] [j]; 25} 26} 27} 28} 29 void display (int array [] [MAXSIZE], int len1, int len2, int size1, int size2) // display the array information 30 {31 for (int I = len1; I <= size1; I ++) 32 {33 for (int j = len2; j <= size2; j ++) 34 {35 cout <array [I] [j] <"\ t"; 36} 37 cout <endl; 38} 39} 40 int * shuchu (int m [], int szcdx, int xhy) // m [] indicates the array to be tested, and szchx indicates the array length, xhy indicates the cyclic condition 41 {42 int t, p; 43 int max, sum; 44 // The cached array is assigned a value of 45 int c [10000]; 46 int v [10000]; 47 int o = 2 * szcdx; 48 int * temp = new int [o]; 49 50 for (t = szcdx-xhy-1; t <szcdx; t ++) 51 {52 c [t-szcdx + xhy + 1] = m [t]; 53} 54 // cycle 55 for (t = xhy; t> = 0; t --) 56 {57 sum = 0; 58 for (p = 0; p <= t; p ++) 59 {60 sum = sum + c [p]; 61} 62 v [t] = sum; 63} 64 // The maximum cyclic output value is 65 max = v [0]; 66 for (t = 0; t <xhy + 1; t ++) 67 {68 if (max <= v [t]) 69 {70 max = v [t]; 71} 72 // printf ("% d ", v [t]); 73 temp [t] = v [t]; 74} 75 return temp; 76} 77 int maxs (int s [], int length) // output maximum 78 {79 int d = s [0]; 80 for (int f = 0; f <length; f ++) 81 {82 if (d <= s [f]) 83 {84 d = s [f]; 85} 86} 87 return d; 88} 89 int main (int argc, char * argv []) 90 {91 int len1, len2; 92 int x [3]; 93 int y [3]; 94 int * k; 95 int * l; 96 int array [MAXSIZE] [MAXSIZE]; 97 read (array, len1, len2); 98 cout <"Matrix:" <endl; 99 display (array, 0, 0, len1-1, len2-1); 100 101 102 103 for (int I = 0; I <3; I ++) 104 {105 x [I] = array [0] [I]; 106 107} 108 int e = 3; 109 int w [6]; 110 int u [6]; 111 int q = 0; 112 printf ("the sum of the first sub-array of the array:"); 113 for (I = 2; I> = 0; I --) 114 {115 116 k = shuchu (x, 3, I); 117 for (int r = 0; r <e; r ++) 118 {119 120 w [q] = k [r]; 121 printf ("% d", w [q]); 122 q ++; 123} 124 e --; 125} 126 for (int j = 0; j <3; j ++) 127 {128 y [j] = array [1] [j]; 129} 130 printf ("\ n"); 131 e = 3; 132 q = 0; 133 printf ("the sum of the Child arrays in the second row of the array :"); 134 for (I = 2; I> = 0; I --) 135 {136 137 l = shuchu (y, 3, I); 138 for (int r = 0; r <e; r ++) 139 {140 141 u [q] = l [r]; 142 printf ("% d", u [q]); 143 q ++; 144} 145 e --; 146} 147 148 149 printf ("\ n"); 150 int h [6]; 151 printf ("the array contains the child arrays of two rows and:"); 152 for (int m = 0; m <6; m ++) 153 {154 h [m] = w [m] + u [m]; 155 printf ("% d", h [m]); 156} 157 158 159 int k1 = maxs (w, 6); 160 int k2 = maxs (u, 6); 161 int k3 = maxs (h, 6 ); 162 163 int maxx = k1; 164 if (maxx <= k2) 165 {166 maxx = k2; 167} 168 if (maxx <= k3) 169 {170 maxx = k3; 171} 172 printf ("\ n Max and % d \ n", maxx); 173 return 0; 174}
V. Calculation Result
5. Experiences
This experiment is a little more difficult than the last one, and the last logic idea is confusing. This time, when the last function is called, There is a logic problem, then there was a problem while reading the file. Most of the credit this time was made by my teammates, especially in logical reasoning, especially more than I thought. Thanks to a good teammate and thanks to Gao Yang.
6. paired development photos