尝试解决项目 euler #11 时,我输出的两个最大产品一直是负数。有点不知所措,我想通过使用 long long 数据类型我会很安全,但我不知道发生了什么。我不知道还能尝试什么,我可能正在做一些非常愚蠢的事情,但我已经在沙滩上旋转了几个小时,我想是时候伸出援手了
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
long long hZ (int matrix[20][20], int i, int j);
int main()
{
fstream inFile;
int matrix [20][20];
long long max = 0;
inFile.open("numbers.txt");
if(!inFile)
cout << "numbers.txt failed to open!" << endl;
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
inFile >> matrix[i][j];
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 16; j++)
{
long long temp = hZ(matrix, i, j);
if (temp > max)
max = temp;
cout << matrix[i][j] << " " << matrix[i][j+1] << " " << matrix[i][j+2] << " " << matrix[i][j+3] << " " << matrix[i][j+4];
//cout.setf(ios::fixed);
cout << " product: " << temp << " max: " << max << endl;
}
cout << endl;
}
cout << "The max is: " << max;
return 0;
}
long long hZ (int matrix[20][20], int i , int j)
{
long long hZ_product = 0;
hZ_product = (matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3] * matrix[i][j+4]);
return hZ_product;
}
输出:(如果您将输出向下滚动到接近尾声,您将看到我对导致问题的行发表评论的位置):
8 2 22 97 38 product: 1297472 max: 1297472
2 22 97 38 15 product: 2432760 max: 2432760
22 97 38 15 0 product: 0 max: 2432760
97 38 15 0 40 product: 0 max: 2432760
38 15 0 40 0 product: 0 max: 2432760
15 0 40 0 75 product: 0 max: 2432760
0 40 0 75 4 product: 0 max: 2432760
40 0 75 4 5 product: 0 max: 2432760
0 75 4 5 7 product: 0 max: 2432760
75 4 5 7 78 product: 819000 max: 2432760
4 5 7 78 52 product: 567840 max: 2432760
5 7 78 52 12 product: 1703520 max: 2432760
7 78 52 12 50 product: 17035200 max: 17035200
78 52 12 50 77 product: 187387200 max: 187387200
52 12 50 77 91 product: 218618400 max: 218618400
12 50 77 91 8 product: 33633600 max: 218618400
49 49 99 40 17 product: 161635320 max: 218618400
49 99 40 17 81 product: 267193080 max: 267193080
99 40 17 81 18 product: 98152560 max: 267193080
40 17 81 18 57 product: 56512080 max: 267193080
17 81 18 57 60 product: 84768120 max: 267193080
81 18 57 60 87 product: 433813320 max: 433813320
18 57 60 87 17 product: 91047240 max: 433813320
57 60 87 17 40 product: 202327200 max: 433813320
60 87 17 40 98 product: 347860800 max: 433813320
87 17 40 98 43 product: 249300240 max: 433813320
17 40 98 43 69 product: 197720880 max: 433813320
40 98 43 69 48 product: 558270720 max: 558270720
98 43 69 48 4 product: 55827072 max: 558270720
43 69 48 4 56 product: 31901184 max: 558270720
69 48 4 56 62 product: 45997056 max: 558270720
48 4 56 62 0 product: 0 max: 558270720
81 49 31 73 55 product: 494001585 max: 558270720
49 31 73 55 79 product: 481804015 max: 558270720
31 73 55 79 14 product: 137658290 max: 558270720
73 55 79 14 29 product: 128777110 max: 558270720
55 79 14 29 93 product: 164058510 max: 558270720
79 14 29 93 71 product: 211784622 max: 558270720
14 29 93 71 40 product: 107232720 max: 558270720
29 93 71 40 67 product: 513185160 max: 558270720
93 71 40 67 53 product: 937890120 max: 937890120
71 40 67 53 88 product: 887465920 max: 937890120
40 67 53 88 30 product: 374985600 max: 937890120
67 53 88 30 3 product: 28123920 max: 937890120
53 88 30 3 49 product: 20568240 max: 937890120
88 30 3 49 13 product: 5045040 max: 937890120
30 3 49 13 36 product: 2063880 max: 937890120
3 49 13 36 65 product: 4471740 max: 937890120
52 70 95 23 4 product: 31813600 max: 937890120
70 95 23 4 60 product: 36708000 max: 937890120
95 23 4 60 11 product: 5768400 max: 937890120
23 4 60 11 42 product: 2550240 max: 937890120
4 60 11 42 69 product: 7650720 max: 937890120
60 11 42 69 24 product: 45904320 max: 937890120
11 42 69 24 68 product: 52024896 max: 937890120
42 69 24 68 56 product: 264854016 max: 937890120
69 24 68 56 1 product: 6306048 max: 937890120
24 68 56 1 32 product: 2924544 max: 937890120
68 56 1 32 56 product: 6823936 max: 937890120
56 1 32 56 71 product: 7124992 max: 937890120
1 32 56 71 37 product: 4707584 max: 937890120
32 56 71 37 2 product: 9415168 max: 937890120
56 71 37 2 36 product: 10592064 max: 937890120
71 37 2 36 91 product: 17212104 max: 937890120
22 31 16 71 51 product: 39512352 max: 937890120
31 16 71 51 67 product: 120333072 max: 937890120
16 71 51 67 63 product: 244547856 max: 937890120
71 51 67 63 89 product: 1360297449 max: 1360297449
51 67 63 89 41 product: 785523879 max: 1360297449
67 63 89 41 92 product: 1417023468 max: 1417023468
63 89 41 92 36 product: 761385744 max: 1417023468
89 41 92 36 54 product: 652616352 max: 1417023468
41 92 36 54 22 product: 161320896 max: 1417023468
92 36 54 22 40 product: 157386240 max: 1417023468
36 54 22 40 40 product: 68428800 max: 1417023468
54 22 40 40 28 product: 53222400 max: 1417023468
22 40 40 28 66 product: 65049600 max: 1417023468
40 40 28 66 33 product: 97574400 max: 1417023468
40 28 66 33 13 product: 31711680 max: 1417023468
28 66 33 13 80 product: 63423360 max: 1417023468
24 47 32 60 99 product: 214410240 max: 1417023468
47 32 60 99 3 product: 26801280 max: 1417023468
32 60 99 3 45 product: 25660800 max: 1417023468
60 99 3 45 2 product: 1603800 max: 1417023468
99 3 45 2 44 product: 1176120 max: 1417023468
3 45 2 44 75 product: 891000 max: 1417023468
45 2 44 75 33 product: 9801000 max: 1417023468
2 44 75 33 53 product: 11543400 max: 1417023468
44 75 33 53 78 product: 450192600 max: 1417023468
75 33 53 78 36 product: 368339400 max: 1417023468
33 53 78 36 84 product: 412540128 max: 1417023468
53 78 36 84 20 product: 250024320 max: 1417023468
78 36 84 20 35 product: 165110400 max: 1417023468
36 84 20 35 17 product: 35985600 max: 1417023468
84 20 35 17 12 product: 11995200 max: 1417023468
20 35 17 12 50 product: 7140000 max: 1417023468
32 98 81 28 64 product: 455196672 max: 1417023468
98 81 28 64 23 product: 327172608 max: 1417023468
81 28 64 23 67 product: 223679232 max: 1417023468
28 64 23 67 10 product: 27614720 max: 1417023468
64 23 67 10 26 product: 25642240 max: 1417023468
23 67 10 26 38 product: 15225080 max: 1417023468
67 10 26 38 40 product: 26478400 max: 1417023468
10 26 38 40 67 product: 26478400 max: 1417023468
26 38 40 67 59 product: 156222560 max: 1417023468
38 40 67 59 54 product: 324462240 max: 1417023468
40 67 59 54 70 product: 597693600 max: 1417023468
67 59 54 70 66 product: 986194440 max: 1417023468
59 54 70 66 18 product: 264947760 max: 1417023468
54 70 66 18 38 product: 170644320 max: 1417023468
70 66 18 38 64 product: 202245120 max: 1417023468
66 18 38 64 70 product: 202245120 max: 1417023468
67 26 20 68 2 product: 4738240 max: 1417023468
26 20 68 2 62 product: 4384640 max: 1417023468
20 68 2 62 12 product: 2023680 max: 1417023468
68 2 62 12 20 product: 2023680 max: 1417023468
2 62 12 20 95 product: 2827200 max: 1417023468
62 12 20 95 63 product: 89056800 max: 1417023468
12 20 95 63 94 product: 135021600 max: 1417023468
20 95 63 94 39 product: 438820200 max: 1417023468
95 63 94 39 63 product: 1382283630 max: 1417023468
63 94 39 63 8 product: 116402832 max: 1417023468
94 39 63 8 40 product: 73906560 max: 1417023468
39 63 8 40 91 product: 71547840 max: 1417023468
63 8 40 91 66 product: 121080960 max: 1417023468
8 40 91 66 49 product: 94174080 max: 1417023468
40 91 66 49 94 product: 1106545440 max: 1417023468
91 66 49 94 21 product: 580936356 max: 1417023468
24 55 58 5 66 product: 25264800 max: 1417023468
55 58 5 66 73 product: 76847100 max: 1417023468
58 5 66 73 99 product: 138324780 max: 1417023468
5 66 73 99 26 product: 62007660 max: 1417023468
66 73 99 26 97 product: 1202948604 max: 1417023468
73 99 26 97 17 product: 309850398 max: 1417023468
99 26 97 17 78 product: 331073028 max: 1417023468
26 97 17 78 78 product: 260845416 max: 1417023468
97 17 78 78 96 product: 963121536 max: 1417023468
17 78 78 96 83 product: 824114304 max: 1417023468
78 78 96 83 14 product: 678682368 max: 1417023468
78 96 83 14 88 product: 765692928 max: 1417023468
96 83 14 88 34 product: 333763584 max: 1417023468
83 14 88 34 89 product: 309426656 max: 1417023468
14 88 34 89 63 product: 234866016 max: 1417023468
88 34 89 63 72 product: 1207882368 max: 1417023468
21 36 23 9 75 product: 11736900 max: 1417023468
36 23 9 75 0 product: 0 max: 1417023468
23 9 75 0 76 product: 0 max: 1417023468
9 75 0 76 44 product: 0 max: 1417023468
75 0 76 44 20 product: 0 max: 1417023468
0 76 44 20 45 product: 0 max: 1417023468
76 44 20 45 35 product: 105336000 max: 1417023468
44 20 45 35 14 product: 19404000 max: 1417023468
20 45 35 14 0 product: 0 max: 1417023468
45 35 14 0 61 product: 0 max: 1417023468
35 14 0 61 33 product: 0 max: 1417023468
14 0 61 33 97 product: 0 max: 1417023468
0 61 33 97 34 product: 0 max: 1417023468
61 33 97 34 31 product: 205805094 max: 1417023468
33 97 34 31 33 product: 111337182 max: 1417023468
97 34 31 33 95 product: 320516130 max: 1417023468
78 17 53 28 22 product: 43291248 max: 1417023468
17 53 28 22 75 product: 41626200 max: 1417023468
53 28 22 75 31 product: 75906600 max: 1417023468
28 22 75 31 67 product: 95957400 max: 1417023468
22 75 31 67 15 product: 51405750 max: 1417023468
75 31 67 15 94 product: 219642750 max: 1417023468
31 67 15 94 3 product: 8785710 max: 1417023468
67 15 94 3 80 product: 22672800 max: 1417023468
15 94 3 80 4 product: 1353600 max: 1417023468
94 3 80 4 62 product: 5594880 max: 1417023468
3 80 4 62 16 product: 952320 max: 1417023468
80 4 62 16 14 product: 4444160 max: 1417023468
4 62 16 14 9 product: 499968 max: 1417023468
62 16 14 9 53 product: 6624576 max: 1417023468
16 14 9 53 56 product: 5983488 max: 1417023468
14 9 53 56 92 product: 34405056 max: 1417023468
16 39 5 42 96 product: 12579840 max: 1417023468
39 5 42 96 35 product: 27518400 max: 1417023468
5 42 96 35 31 product: 21873600 max: 1417023468
42 96 35 31 47 product: 205611840 max: 1417023468
96 35 31 47 55 product: 269253600 max: 1417023468
35 31 47 55 58 product: 162674050 max: 1417023468
31 47 55 58 88 product: 409009040 max: 1417023468
47 55 58 88 24 product: 316652160 max: 1417023468
55 58 88 24 0 product: 0 max: 1417023468
58 88 24 0 17 product: 0 max: 1417023468
88 24 0 17 54 product: 0 max: 1417023468
24 0 17 54 24 product: 0 max: 1417023468
0 17 54 24 36 product: 0 max: 1417023468
17 54 24 36 29 product: 23001408 max: 1417023468
54 24 36 29 85 product: 115007040 max: 1417023468
24 36 29 85 57 product: 121396320 max: 1417023468
86 56 0 48 35 product: 0 max: 1417023468
56 0 48 35 71 product: 0 max: 1417023468
0 48 35 71 89 product: 0 max: 1417023468
48 35 71 89 7 product: 74311440 max: 1417023468
35 71 89 7 5 product: 7740775 max: 1417023468
71 89 7 5 44 product: 9731260 max: 1417023468
89 7 5 44 44 product: 6030640 max: 1417023468
7 5 44 44 37 product: 2507120 max: 1417023468
5 44 44 37 44 product: 15759040 max: 1417023468
44 44 37 44 60 product: 189108480 max: 1417023468
44 37 44 60 21 product: 90256320 max: 1417023468
37 44 60 21 58 product: 118974240 max: 1417023468
44 60 21 58 51 product: 163991520 max: 1417023468
60 21 58 51 54 product: 201262320 max: 1417023468
21 58 51 54 17 product: 57024324 max: 1417023468
58 51 54 17 58 product: 157495752 max: 1417023468
19 80 81 68 5 product: 41860800 max: 1417023468
80 81 68 5 94 product: 207100800 max: 1417023468
81 68 5 94 47 product: 121671720 max: 1417023468
68 5 94 47 69 product: 103646280 max: 1417023468
5 94 47 69 28 product: 42677880 max: 1417023468
94 47 69 28 73 product: 623097048 max: 1417023468
47 69 28 73 92 product: 609839664 max: 1417023468
69 28 73 92 13 product: 168679056 max: 1417023468
28 73 92 13 86 product: 210237664 max: 1417023468
73 92 13 86 52 product: 390441376 max: 1417023468
92 13 86 52 17 product: 90924704 max: 1417023468
13 86 52 17 77 product: 76100024 max: 1417023468
86 52 17 77 4 product: 23415392 max: 1417023468
52 17 77 4 89 product: 24232208 max: 1417023468
17 77 4 89 55 product: 25630220 max: 1417023468
77 4 89 55 40 product: 60306400 max: 1417023468
4 52 8 83 97 product: 13396864 max: 1417023468
52 8 83 97 35 product: 117222560 max: 1417023468
8 83 97 35 99 product: 223173720 max: 1417023468
83 97 35 99 16 product: 446347440 max: 1417023468
97 35 99 16 7 product: 37643760 max: 1417023468
35 99 16 7 97 product: 37643760 max: 1417023468
99 16 7 97 57 product: 61305552 max: 1417023468
16 7 97 57 32 product: 19815936 max: 1417023468
7 97 57 32 16 product: 19815936 max: 1417023468
97 57 32 16 26 product: 73602048 max: 1417023468
57 32 16 26 26 product: 19728384 max: 1417023468
32 16 26 26 79 product: 27342848 max: 1417023468
16 26 26 79 33 product: 28197312 max: 1417023468
26 26 79 33 27 product: 47582964 max: 1417023468
26 79 33 27 98 product: 179351172 max: 1417023468
79 33 27 98 66 product: 455276052 max: 1417023468
88 36 68 87 57 product: 1068287616 max: 1417023468
36 68 87 57 62 product: 752657184 max: 1417023468
68 87 57 62 20 product: 418142880 max: 1417023468
87 57 62 20 72 product: 442739520 max: 1417023468
57 62 20 72 3 product: 15266880 max: 1417023468
62 20 72 3 46 product: 12320640 max: 1417023468
20 72 3 46 33 product: 6557760 max: 1417023468
72 3 46 33 67 product: 21968496 max: 1417023468
3 46 33 67 46 product: 14035428 max: 1417023468
46 33 67 46 55 product: 257316180 max: 1417023468
33 67 46 55 12 product: 67125960 max: 1417023468
67 46 55 12 32 product: 65091840 max: 1417023468
46 55 12 32 63 product: 61205760 max: 1417023468
55 12 32 63 93 product: 123742080 max: 1417023468
12 32 63 93 53 product: 119242368 max: 1417023468
32 63 93 53 69 product: 685643616 max: 1417023468
4 42 16 73 38 product: 7456512 max: 1417023468
42 16 73 38 25 product: 46603200 max: 1417023468
16 73 38 25 39 product: 43274400 max: 1417023468
73 38 25 39 11 product: 29751150 max: 1417023468
38 25 39 11 24 product: 9781200 max: 1417023468
25 39 11 24 94 product: 24195600 max: 1417023468
39 11 24 94 72 product: 69683328 max: 1417023468
11 24 94 72 18 product: 32161536 max: 1417023468
24 94 72 18 8 product: 23390208 max: 1417023468
94 72 18 8 46 product: 44831232 max: 1417023468
72 18 8 46 29 product: 13830912 max: 1417023468
18 8 46 29 32 product: 6147072 max: 1417023468
8 46 29 32 40 product: 13660160 max: 1417023468
46 29 32 40 62 product: 105866240 max: 1417023468
29 32 40 62 76 product: 174909440 max: 1417023468
32 40 62 76 36 product: 217128960 max: 1417023468
20 69 36 41 72 product: 146655360 max: 1417023468
69 36 41 72 30 product: 219983040 max: 1417023468
36 41 72 30 23 product: 73327680 max: 1417023468
41 72 30 23 88 product: 179245440 max: 1417023468
72 30 23 88 34 product: 148642560 max: 1417023468
30 23 88 34 62 product: 127997760 max: 1417023468
23 88 34 62 99 product: 422392608 max: 1417023468
88 34 62 99 69 product: 1267177824 max: 1417023468
34 62 99 69 82 product: 1180779336 max: 1417023468
62 99 69 82 67 product: -1968137428 max: 1417023468 //ISSUE HERE
99 69 82 67 59 product: -2080725970 max: 1417023468 //ISSUE HERE
69 82 67 59 85 product: 1901116290 max: 1901116290
82 67 59 85 74 product: 2038878340 max: 2038878340
67 59 85 74 4 product: 99457480 max: 2038878340
59 85 74 4 36 product: 53439840 max: 2038878340
85 74 4 36 16 product: 14492160 max: 2038878340
20 73 35 29 78 product: 115588200 max: 2038878340
73 35 29 78 31 product: 179161710 max: 2038878340
35 29 78 31 90 product: 220884300 max: 2038878340
29 78 31 90 1 product: 6310980 max: 2038878340
78 31 90 1 74 product: 16103880 max: 2038878340
31 90 1 74 31 product: 6400260 max: 2038878340
90 1 74 31 49 product: 10116540 max: 2038878340
1 74 31 49 71 product: 7980826 max: 2038878340
74 31 49 71 48 product: 383079648 max: 2038878340
31 49 71 48 86 product: 445200672 max: 2038878340
49 71 48 86 81 product: 1163266272 max: 2038878340
71 48 86 81 16 product: 379842048 max: 2038878340
48 86 81 16 23 product: 123047424 max: 2038878340
86 81 16 23 57 product: 146118816 max: 2038878340
81 16 23 57 5 product: 8495280 max: 2038878340
16 23 57 5 54 product: 5663520 max: 2038878340
1 70 54 71 83 product: 22275540 max: 2038878340
70 54 71 83 51 product: 1136052540 max: 2038878340
54 71 83 51 54 product: 876383388 max: 2038878340
71 83 51 54 69 product: 1119823218 max: 2038878340
83 51 54 69 16 product: 252354528 max: 2038878340
51 54 69 16 92 product: 279718272 max: 2038878340
54 69 16 92 33 product: 180994176 max: 2038878340
69 16 92 33 48 product: 160883712 max: 2038878340
16 92 33 48 61 product: 142230528 max: 2038878340
92 33 48 61 43 product: 382244544 max: 2038878340
33 48 61 43 52 product: 216051264 max: 2038878340
48 61 43 52 1 product: 6547008 max: 2038878340
61 43 52 1 89 product: 12139244 max: 2038878340
43 52 1 89 19 product: 3781076 max: 2038878340
52 1 89 19 67 product: 5891444 max: 2038878340
1 89 19 67 48 product: 5438256 max: 2038878340
The max is: 2038878340