Project Euler:problem 5-gon Ring

Source: Internet
Author: User

Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and all 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 is described uniquely. For example, the above solution can is 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, ten, one, and 12. There is 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 All group it is possible to form 9-digit strings; The maximum string for a 3-gon ring is 432621513.

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



The a0 of the most outer ring is the smallest number in the outer ring, so the a0<=6, at the same time, makes the string the largest, so a0=6, while the other number of the outer ring is 7,8,9,10 respectively.

The number of 1+2+3+...+9+10=55 inner rings is 1,2,3,4,5, and the 14 of each row is counted.

So what line A0 is in, the other two numbers are 8, and 6,7 are in the outer ring, so these two numbers can only be 5 and 3.




#include <iostream> #include <string> #include <vector> #include <algorithm>using namespace std ; int main () {int a[7] = {1, 2, 4, 7, 8, 9,};int b[2] = {3, 5};string tmp,res;do{for (int i = 0; i < 2; i++) {if (a [0] + b[i] + a[1] = && a[1] + a[2] + a[3] = = && A[3] + a[4] + a[5] = [+] && a[5] + a[6] + b [(i + 1)% 2] = =) {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] &l t;< 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;}


Stole a lazy, output two sets of results



Apparently that big 6531031914842725.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project Euler:problem 5-gon Ring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.