Project Euler:Problem 68 Magic 5-gon ring

Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.

Working clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely.
For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.

It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.

Total Solution Set
9 4,2,3; 5,3,1; 6,1,2
9 4,3,2; 6,2,1; 5,1,3
10 2,3,5; 4,5,1; 6,1,3
10 2,5,3; 6,3,1; 4,1,5
11 1,4,6; 3,6,2; 5,2,4
11 1,6,4; 5,4,2; 3,2,6
12 1,5,6; 2,6,4; 3,4,5
12 1,6,5; 3,5,4; 2,4,6

By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.

Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum 16-digit string for a "magic" 5-gon
ring?

最外圈的a0是最外圈里最小的数,所以a0<=6,同时要使得这个string最大,所以a0=6,同时最外圈的其他数分别为7,8,9,10。

1+2+3+...+9+10=55         内圈的数为1,2,3,4,5,算起来每一行的和都为14

所以a0所在的哪一行,其他两个数的和为8,而6,7都位于最外圈,所以这两个数只能是5和3

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

int main()
{
	int a[7] = { 1, 2, 4, 7, 8, 9, 10 };
	int b[2] = { 3, 5 };
	string tmp,res;
	do
	{
		for (int i = 0; i < 2; i++)
		{
			if (a[0] + b[i] + a[1] == 14 && a[1] + a[2] + a[3] == 14 && a[3] + a[4] + a[5] == 14 && a[5] + a[6] + b[(i + 1) % 2] == 14)
			{
				cout << "6" << b[i] << b[(i + 1) % 2] << endl;
				cout << a[0] << b[(i + 1) % 2] << a[1] << endl;
				cout << a[2] << a[1] << a[3] << endl;
				cout << a[4] << a[3] << a[5] << endl;
				cout << a[6] << a[5] << b[i] << endl;
				cout << endl;
			}
		}

	} while (next_permutation(a, a + 7));

	system("pause");
	return 0;
}

偷了个懒,输出两组结果

显然那个大的6531031914842725

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

时间: 2025-01-02 02:46:26

Project Euler:Problem 68 Magic 5-gon ring的相关文章

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

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 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

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 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9

Project Euler:Problem 78 Coin partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7. OOOOO OOOO   O OOO   OO OOO   O   O OO   OO   O OO   O   O 

Project Euler:Problem 55 Lychrel numbers

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Al