ProjectEuler 11

对一个20*20的矩阵,求斜对角或者竖行或者横行连续4个数字的最大乘积

思想:

只会很笨的解法,读入文件到一个二维数组,然后对数组中的每个数字,进行4种可能的计算。。。

官网没参考答案。。。

代码:

View Code
 1 private static int gridproduct(String s) {
 2         int product = 0;
 3         int[][] a = readFile(s);
 4         int rows = a.length;
 5         int cols = a[0].length;
 6         for (int i = 0; i < rows; i++)
 7             for (int j = 0; j < cols; j++) {
 8                 int tmp;
 9                 if (i < rows - 3) {
10                     tmp = a[i][j] * a[i + 1][j] * a[i + 2][j] * a[i + 3][j];
11                     if (tmp > product)
12                         product = tmp;
13                 }
14                 ;
15                 if (j < cols - 3) {
16                     tmp = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
17                     if (tmp > product)
18                         product = tmp;
19                 }
20                 if (i < rows - 3 && j < cols - 3) {
21                     tmp = a[i][j] * a[i + 1][j + 1] * a[i + 2][j + 2]
22                             * a[i + 3][j + 3];
23                     if (tmp > product)
24                         product = tmp;
25                 }
26                 if (i > 3 && j < cols - 3) {
27                     tmp = a[i][j] * a[i - 1][j + 1] * a[i - 2][j + 2]
28                             * a[i - 3][j + 3];
29                     if (tmp > product)
30                         product = tmp;
31                 }
32             }
33 
34         return product;
35     }
原文地址:https://www.cnblogs.com/lake19901126/p/3073465.html