Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3

7 4

4 6

8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75

95 64

17 47 82

18 35 87 10

20 04 82 47 65

19 01 23 75 03 34

88 02 77 73 07 63 67

99 65 04 28 06 16 70 92

41 41 26 56 83 40 80 70 33

41 48 72 33 47 32 37 16 94 29

53 71 44 65 25 43 91 52 97 51 14

70 11 33 28 77 73 17 78 39 68 17 57

91 71 52 38 17 14 91 43 58 50 27 29 48

63 66 04 68 89 53 67 30 73 16 69 87 40 31

04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem
67
, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

安装这题目的说法,可以暴力搜索来做

自顶向下:

#include <iostream>
using namespace std;

int main()
{
	int s[15][15];
	for (int i = 0; i < 15; i++)
	{
		for (int j = 0; j <= i; j++)
			cin >> s[i][j];
	}
	for (int i = 1; i < 15; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			if (j == 0)
			{
				s[i][j] += s[i - 1][j];
			}
			else if (i == j)
				s[i][j] += s[i - 1][j - 1];
			else
			{
				int tp = s[i - 1][j - 1] > s[i - 1][j] ? s[i - 1][j - 1] : s[i - 1][j];
				s[i][j] += tp;
			}
		}
	}
	int res = 0;
	for (int i = 0; i < 15; i++)
	{
		if (res < s[14][i])
			res = s[14][i];
	}
	cout << res << endl;
	system("pause");
	return 0;
}

自底向上:

#include <iostream>
using namespace std;

int main()
{
	int s[15][15];
	for (int i = 0; i < 15; i++)
	{
		for (int j = 0; j <= i; j++)
			cin >> s[i][j];
	}
	for (int i = 14; i >0; i--)
	{
		for (int j = 0; j < i; j++)
		{
			int tp = s[i][j]>s[i][j + 1] ? s[i][j] : s[i][j + 1];
			s[i - 1][j] += tp;
		}
	}
	cout << s[0][0] << endl;
	system("pause");
	return 0;
}
时间: 2024-10-11 13:03:10

Project Euler:Problem 18 Maximum path sum I的相关文章

Project Euler:Problem 67 Maximum path sum II

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (righ

Project Euler:Problem 56 Powerful digit sum

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the fo

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

Project Euler:Problem 16 Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? #include <iostream> #include <string> using namespace std; int main() { string s = "1"; for (int i = 1; i <= 1000;

Project Euler:Problem 20 Factorial digit sum

n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! #include <iostream> #include &

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

Project Euler:Problem 82 Path sum: three ways

The minimal path sum in the 5 by 5 matrix below, by starting in any cell in the left column and finishing in any cell in the right column, and only moving up, down, and right, is indicated in red and bold; the sum is equal to 994. ????????13120163053