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/archive/2013/05/11/3073465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值