project euler 68

来源:互联网 发布:在淘宝卖梨膏 编辑:程序博客网 时间:2024/05/17 01:18

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.

TotalSolution Set94,2,3; 5,3,1; 6,1,294,3,2; 6,2,1; 5,1,3102,3,5; 4,5,1; 6,1,3102,5,3; 6,3,1; 4,1,5111,4,6; 3,6,2; 5,2,4111,6,4; 5,4,2; 3,2,6121,5,6; 2,6,4; 3,4,5121,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?


魔力五边形环

考虑下面这个“魔力”三角形环,在其中填入1至6这6个数,每条线上的三个数加起来都是9。

从最外侧结点所填的数最小的线(在这个例子中是4,3,2)开始,按顺时针方向,每个解都能被唯一表述。例如,上面这个解可以记作解集:4,3,2; 6,2,1; 5,1,3。

将环填满后,每条线上的总和一共有四种可能:9、10、11和12。总共有8种填法:

总和解集94,2,3; 5,3,1; 6,1,294,3,2; 6,2,1; 5,1,3102,3,5; 4,5,1; 6,1,3102,5,3; 6,3,1; 4,1,5111,4,6; 3,6,2; 5,2,4111,6,4; 5,4,2; 3,2,6121,5,6; 2,6,4; 3,4,5121,6,5; 3,5,4; 2,4,6

把解集中的数字连接起来,可以构造一个9位数字串;对于三角形环来说,最大的数字串是432621513。

在如下的“魔力”五边形环中,在其中填入1至10这10个数,根据不同的填写方式,可以组成16位或17位数字串。在“魔力”五边形环中,最大的16位数字串是多少?

package projecteuler;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import junit.framework.TestCase;public class Prj68 extends TestCase {public void qtestMagic3GonRing() {int x1 = 0;int x2 = 0;int x3 = 0;int x4 = 0;int x5 = 0;int x6 = 0;int[] flag = new int[6];for (x1 = 1; x1 <= 6; x1++) {flag[0] = x1;for (x2 = 1; x2 <= 6; x2++) {if (x2 == flag[0]) {continue;}flag[1] = x2;for (x3 = 1; x3 <= 6; x3++) {if (x3 == flag[0] || x3 == flag[1]) {continue;}flag[2] = x3;for (x4 = 1; x4 <= 6; x4++) {if (x4 == flag[0] || x4 == flag[1] || x4 == flag[2]) {continue;}flag[3] = x4;for (x5 = 1; x5 <= 6; x5++) {if (x5 == flag[0] || x5 == flag[1] || x5 == flag[2]|| x5 == flag[3]) {continue;}flag[4] = x5;for (x6 = 1; x6 <= 6; x6++) {if (x6 == flag[0] || x6 == flag[1]|| x6 == flag[2] || x6 == flag[3]|| x6 == flag[4]) {continue;}flag[5] = x6;if ((x1 + x4 + x5) == (x2 + x5 + x6)&& (x3 + x4 + x6) == (x2 + x5 + x6)) {Set<Integer> set = new HashSet<Integer>();set.add(x1);set.add(x2);set.add(x3);set.add(x4);set.add(x5);set.add(x6);if (set.size() == 6) {String fstr = "%d,%d,%d;%d,%d,%d;%d,%d,%d";fstr = String.format(fstr, x1, x4, x5,x2, x5, x6, x3, x6, x4);System.out.println(fstr);}}}}}}}}}public void testMagic5GonRing() {int x1 = 0;int x2 = 0;int x3 = 0;int x4 = 0;int x5 = 0;int x6 = 0;int x7 = 0;int x8 = 0;int x9 = 0;int x10 = 0;int[] flag = new int[10];List<String> result = new ArrayList<String>();for (x1 = 1; x1 <= 10; x1++) {flag[0] = x1;for (x2 = 1; x2 <= 10; x2++) {if (x2 == flag[0]) {continue;}flag[1] = x2;for (x3 = 1; x3 <= 10; x3++) {if (x3 == flag[0] || x3 == flag[1]) {continue;}flag[2] = x3;for (x4 = 1; x4 <= 10; x4++) {if (x4 == flag[0] || x4 == flag[1] || x4 == flag[2]) {continue;}flag[3] = x4;for (x5 = 1; x5 <= 10; x5++) {if (x5 == flag[0] || x5 == flag[1] || x5 == flag[2]|| x5 == flag[3]) {continue;}flag[4] = x5;for (x6 = 1; x6 <= 10; x6++) {if (x6 == flag[0] || x6 == flag[1]|| x6 == flag[2] || x6 == flag[3]|| x6 == flag[4]) {continue;}flag[5] = x6;for (x7 = 1; x7 <= 10; x7++) {if (x7 == flag[0] || x7 == flag[1]|| x7 == flag[2] || x7 == flag[3]|| x7 == flag[4] || x7 == flag[5]) {continue;}flag[6] = x7;for (x8 = 1; x8 <= 10; x8++) {if (x8 == flag[0] || x8 == flag[1]|| x8 == flag[2]|| x8 == flag[3]|| x8 == flag[4]|| x8 == flag[5]|| x8 == flag[6]) {continue;}flag[7] = x8;for (x9 = 1; x9 <= 10; x9++) {if (x9 == flag[0] || x9 == flag[1]|| x9 == flag[2]|| x9 == flag[3]|| x9 == flag[4]|| x9 == flag[5]|| x9 == flag[6]|| x9 == flag[7]) {continue;}flag[8] = x9;for (x10 = 1; x10 <= 10; x10++) {if (x10 == flag[0]|| x10 == flag[1]|| x10 == flag[2]|| x10 == flag[3]|| x10 == flag[4]|| x10 == flag[5]|| x10 == flag[6]|| x10 == flag[7]|| x10 == flag[8]) {continue;}flag[9] = x10;if ((x1 + x6 + x7) == (x2 + x7 + x8)&& (x1 + x6 + x7) == (x3+ x8 + x9)&& (x1 + x6 + x7) == (x4+ x9 + x10)&& (x1 + x6 + x7) == (x5+ x10 + x6)) {Set<Integer> set = new HashSet<Integer>();set.add(x1);set.add(x2);set.add(x3);set.add(x4);set.add(x5);set.add(x6);set.add(x7);set.add(x8);set.add(x9);set.add(x10);if (set.size() == 10) {String fstr = "%d,%d,%d;%d,%d,%d;%d,%d,%d,%d,%d,%d;%d,%d,%d";fstr = String.format(fstr, x1, x6,x7, x2, x7, x8,x3, x8, x9, x4,x9, x10, x5,x10, x6);if (x1 >= 6 && x2 > 6&& x3 > 6&& x4 > 6&& x5 > 6) {result.add(fstr);}}}}}}}}}}}}}for (String str : result) {System.out.println(str);}}}


0 0
原创粉丝点击