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

4 6

8 5 9 3

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

Find the maximum total from top to bottom in triangle.txt (right
click and ‘Save Link/Target As...‘), a 15K text file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as
there are 299 altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)

动态规划

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main()
{
	ifstream input;
	string line;
	input.open("triangle.txt");
	vector<vector<int>>data;
	vector<int>num;
	while (getline(input, line))
	{
		num.clear();
		int tmp = 0;
		for (int i = 0; i < line.length(); i++)
		{
			if (i % 3 == 2)
				continue;
			if (i % 3 == 1)
			{
				tmp = tmp * 10 + line[i] - '0';
				num.push_back(tmp);
				tmp = 0;
			}
			else
				tmp += line[i] - '0';
		}
		data.push_back(num);
	}
	input.close();

	for (int i = data.size() - 2; i >= 0; i--)
	{
		for (int j = 0; j < data[i].size(); j++)
		{
			data[i][j] = data[i + 1][j]>data[i + 1][j + 1] ? data[i][j] + data[i + 1][j] : data[i][j] + data[i + 1][j + 1];
		}
	}
	cout << data[0][0] << endl;
	system("pause");
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 21:19:48

Project Euler:Problem 67 Maximum path sum II的相关文章

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 2 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

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

[lintcode] Binary Tree Maximum Path Sum II

Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree and contain at least one node in it. 给一棵二叉树,找出从根节点出发的路径中,和最大的一条. 这条路径可以在任何二叉树中的节点结束,但是必须包含至少一个点(也就是根了). /** * Definition of TreeNode: * public class Tr