---恢复内容开始---

题目链接

从三角数开始,循环到八角数,再到三角数,求这6个数的和

这个比较复杂,代码在网上找的

Java:

package project61;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set; public class P61
{
static enum Type {
TRIANGLE, SQUARE, PENTAGONAL, HEXAGONAL, HEPTAGONAL, OCTAGONAL;
} static class Suffix {
public Type type;
public int val; public Suffix(Type type, int val) {
this.type = type;
this.val = val;
}
} static class State {
public List<Integer> vals;
public Set<Type> used;
public int suffix; public State(int starting) {
this.vals = new ArrayList<Integer>();
this.used = new HashSet<Type>();
this.suffix = starting % 100;
this.vals.add(starting);
this.used.add(Type.OCTAGONAL);
} public State(State state, Suffix newval) {
this.vals = new ArrayList<Integer>(state.vals);
this.used = new HashSet<Type>(state.used);
this.used.add(newval.type);
this.vals.add(state.suffix * 100 + newval.val);
this.suffix = newval.val;
}
} public Map<Integer, Set<Suffix>> prefixmap = new HashMap<Integer, Set<Suffix>>(); public P61() throws Exception
{
Queue<State> search = new LinkedList<State>(); int n = 1;
boolean bounds = true;
while (bounds) {
int tri = n * (n + 1) / 2;
int sq = n * n;
int pent = n * (3 * n - 1) / 2;
int hex = n * (2 * n - 1);
int hept = n * (5 * n - 3) / 2;
int oct = n * (3 * n - 2); if (tri < 10000 && tri >= 1000)
addPrefix(tri / 100, new Suffix(Type.TRIANGLE, tri % 100));
if (sq < 10000 && sq >= 1000)
addPrefix(sq / 100, new Suffix(Type.SQUARE, sq % 100));
if (pent < 10000 && pent >= 1000)
addPrefix(pent / 100, new Suffix(Type.PENTAGONAL, pent % 100));
if (hex < 10000 && hex >= 1000)
addPrefix(hex / 100, new Suffix(Type.HEXAGONAL, hex % 100));
if (hept < 10000 && hept >= 1000)
addPrefix(hept / 100, new Suffix(Type.HEPTAGONAL, hept % 100));
if (oct < 10000 && oct >= 1000)
search.add(new State(oct)); bounds &= (tri < 10000);
n++;
} while (search.size() > 0) {
State cur = search.poll();
// System.out.println(cur);
if (cur.vals.size() == 6 && cur.used.size() == 6 &&
(cur.vals.get(0) / 100 == cur.vals.get(5) % 100)) {
int sum = 0;
for (int val : cur.vals) {
System.out.println(val);
sum += val;
}
System.out.println();
System.out.println(sum);
} else {
Set<Suffix> candidates = prefixmap.get(cur.suffix);
if (candidates != null) {
for (Suffix suff : candidates) {
if (!cur.used.contains(suff.type)) {
State newstate = new State(cur, suff);
search.add(newstate);
}
}
}
}
}
} public void addPrefix(int prefix, Suffix value) {
if (!prefixmap.containsKey(prefix)) {
prefixmap.put(prefix, new HashSet<Suffix>());
}
prefixmap.get(prefix).add(value);
} public static void main(String[] args) throws Exception
{
new P61();
} }

Python:

代码1:

def main(p):

    Tri = lambda n: (n * (n + 1)) / 2
Squ = lambda n: (n * n)
Pen = lambda n: (n * (3 * n - 1)) / 2
Hex = lambda n: (n * (2 * n - 1))
Hep = lambda n: (n * (5 * n - 3)) / 2
Oct = lambda n: (n * (3 * n - 2))
a = [[Tri, Squ, Pen, Hex, Hep, Oct][i] for i in p]
S = [] for fun in a:
S.append([[str(fun(i))]
for i in range(1000) if len(str(fun(i))) == 4]) ans = [S[0][:]]
for t in S[1:]:
ans.append([])
for j in ans[-2]:
for i in t:
if j[-1][2:] == i[0][:2]:
ans[-1].append(j + i)
for i in ans[5]:
if i[0][:2] == i[-1][2:]:
print sum(map(int,i)) def dfs(p, l):
r = len(p)
if l == r:
main(p)
else:
for i in range(l, r):
p[l] ,p[i] = p[i], p[l]
dfs(p, l + 1)
p[l] ,p[i] = p[i], p[l] p = [0,1,2,3,4,5]
dfs(p, 1)

根据dfs写的。。。。。

代码2:

from itertools import permutations

def trig(n):
return n*(n+1)//2 def quad(n):
return n*n def penta(n):
return n*(3*n-1)//2 def hexa(n):
return n*(2*n-1) def hepta(n):
return n*(5*n-3)//2 def octo(n):
return n*(3*n-2) def ajout(d,k,x):
try:
d[k].append(x)
except:
d[k]=[x] listef=[trig,quad,penta,hexa,hepta,octo] listedict=[dict() for i in range(6)] listenb=[[f(n) for n in range(150) if f(n)>=1000 and f(n)<=9999 and str(f(n))[-2]!=''] for f in listef] print listenb for i in range(6):
for x in listenb[i]:
ajout(listedict[i],x//100,x) print listedict liste_possibilites=[] for p in permutations([0,1,2,3,4]):
for x in listenb[-1]:
chaines=[[x]]
for i in range(5):
chaines2=[]
for c in chaines:
try:
nb=c[-1]
listecontinuation=listedict[p[i]][nb%100]
for y in listecontinuation:
chaines2.append(c+[y])
except:
continue
chaines=chaines2
liste_possibilites+=chaines print liste_possibilites solutions=[x for x in liste_possibilites if x[-1]%100==x[0]//100] solution=solutions[0] print(sum(solution))

1.求出所以的三角数到八角数

2.前两位相同的放在一起

3.循环的放在一起。

欧拉工程第61题:Cyclical figurate numbers的更多相关文章

  1. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  2. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  3. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  4. 欧拉工程第67题:Maximum path sum II

    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...

  5. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  6. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  7. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  8. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...

  9. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

随机推荐

  1. PHP流程控制语句下

    ok,继续搞. while循环: while(expr){ 程序块: } 条件expr成立执行程序块,否则结束. do while循环:do while循环比while循环要多循环一次,它要先执行程序 ...

  2. Linux驱动开发之开篇--HelloWorld

    Linux驱动的编写,大致分为两个过程,第一个过程为测试阶段,即为某一具体的设备,添加必要的驱动模块,为了节省编译时间,需要将代码单独放在一处,在编译时,只需要要调用内核的头文件即可:第二个过程为布置 ...

  3. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  4. C语言文件函数

    FILE *fp: 其中的FILE应该大写,它实际上是系统定义的一个结构,在stdio.h文件中.该结构中有文件名,文件状态,文件当前的读写信息等. fp是指向FILE结构的指针变量,通过fp可以找到 ...

  5. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...

  6. String面试题

    //a b c 分别是怎么存储的, a和b a和c分别有什么区别// c和d的区别是什么 String a= "hello";String b= "hello" ...

  7. iOS中的堆(heap)和栈(stack)的理解

    操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segme ...

  8. ios6 处理内存警告

    iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...

  9. cocos2dx中的实现地图卷动的两种方式

    在游戏当中,实现地图卷动是最基本的功能,具体的实现的方法,大致有两类: 方法一:加载两张图片,轮流显示, 优点: 1.无论是地图上下卷动,还是左右卷动都可以 2.支持各种图片,(png,jpg...) ...

  10. 设计模式Builder(建造者)模式

    1.出现原因 在软件系统中,有时候会面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂的对象的各个部分可能面临着剧烈的变化,但是把他们组合在一起的算法 ...