project euler 68

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?


魔力五边形环

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

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

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

总和 解集
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

把解集中的数字连接起来,可以构造一个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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值