Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08

49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00

81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65

52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91

22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80

24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50

32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70

67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21

24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72

21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95

78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92

16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57

86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58

19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40

04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66

88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69

04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36

20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16

20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54

01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

#include <iostream>
using namespace std;

int main()
{
	int s[20][20];
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 20; j++)
			cin >> s[i][j];
	}
	int max = 1;
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 16; j++)
		{
			max = s[i][j] * s[i][j + 1] * s[i][j + 2] * s[i][j + 3]>max ? s[i][j] * s[i][j + 1] * s[i][j + 2] * s[i][j + 3] : max;
		}
	}
	for (int i = 0; i < 16; i++)
	{
		for (int j = 0; j < 20; j++)
		{
			max = s[i][j] * s[i + 1][j] * s[i + 2][j] * s[i + 3][j]>max ? s[i][j] * s[i + 1][j] * s[i + 2][j] * s[i + 3][j] : max;
		}
	}
	for (int i = 0; i < 16; i++)
	{
		for (int j = 0; j < 16; j++)
		{
			max = s[i][j] * s[i + 1][j + 1] * s[i + 2][j + 2] * s[i + 3][j + 3]>max ? s[i][j] * s[i + 1][j + 1] * s[i + 2][j + 2] * s[i + 3][j + 3] : max;
		}
	}
	for (int i = 0; i < 16; i++)
	{
		for (int j = 4; j < 20; j++)
		{
			max = s[i][j] * s[i + 1][j - 1] * s[i + 2][j - 2] * s[i + 3][j - 3]>max ? s[i][j] * s[i + 1][j - 1] * s[i + 2][j - 2] * s[i + 3][j - 3] : max;
		}
	}
	cout << max << endl;
	system("pause");
	return 0;
}
时间: 2024-10-20 16:10:46

Project Euler:Problem 11 Largest product in a grid的相关文章

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagore

R语言学习——欧拉计划(11)Largest product in a grid

Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler: Problem 9 Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 首先联立式子,消除c,得到关于ab的等式5

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be

Project Euler problem 68

题意需要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.并且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i

Project Euler:Problem 12 Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the fir

Project Euler:99 Largest exponential C++

Comparing two numbers written in index form like 211 and 37 is not difficult, as any calculator would confirm that 211 = 2048 < 37 = 2187. However, confirming that 632382518061 > 519432525806 would be much more difficult, as both numbers contain ove