zoukankan      html  css  js  c++  java
  • Project Euler 90:Cube digit pairs 立方体数字对

    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 could be formed:

    In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.

    For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube.

    However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09.

    In determining a distinct arrangement we are interested in the digits on each cube, not the order.

    {1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5}
    {1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9}

    But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers.

    How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?


    立方体数字对

    在一个立方体的六个面上分别标上不同的数字(从0到9),对另一个立方体也如法炮制。将这两个立方体按不同的方向并排摆放,我们可以得到各种各样的两位数。

    例如,平方数64可以通过这样摆放获得:

    事实上,通过仔细地选择两个立方体上的数字,我们可以摆放出所有小于100的平方数:01、04、09、16、25、36、49、64和81。

    例如,其中一种方式就是在一个立方体上标上{0, 5, 6, 7, 8, 9},在另一个立方体上标上{1, 2, 3, 4, 8, 9}。

    在这个问题中,我们允许将标有6或9的面颠倒过来互相表示,只有这样,如{0, 5, 6, 7, 8, 9}和{1, 2, 3, 4, 6, 7}这样本来无法表示09的标法,才能够摆放出全部九个平方数。

    在考虑什么是不同的标法时,我们关注的是立方体上有哪些数字,而不关心它们的顺序。

    {1, 2, 3, 4, 5, 6}等价于{3, 6, 4, 1, 2, 5}
    {1, 2, 3, 4, 5, 6}不同于{1, 2, 3, 4, 5, 9}

    但因为我们允许在摆放两位数时将6和9颠倒过来互相表示,这个例子中的两个不同的集合都可以代表拓展集{1, 2, 3, 4, 5, 6, 9}。

    对这两个立方体有多少中不同的标法可以摆放出所有的平方数?

    解题

     我发现这个翻译我理解不透

    在两个六面体上面涂:0-9的数字,主要这里有10个数字,只用其中6个图,两个六面体涂的数字可以不一样的,6可以当9用,9可以当6用。

    两个六面体上面的数字能组合成:1-9的平方:01 04 09 16 25 36 49 64 81  ,求这样的涂法有多少种?

    骰子说成六面体还吊的。

    0-9 十个数 取出6个 就是一个骰子的涂法。

    先组合出涂法的种类。

    再判断是否能组成1-9的平方

    参考程序 

    # coding=gbk
    
    import time as time 
    from itertools import combinations  
    def run():
        dice=list(combinations([0,1,2,3,4,5,6,7,8,6],6))
        ans = 0 
        for i1,d1 in enumerate(dice):
            for d2 in dice[i1:]:
                if valid(d1,d2) == True:
                    ans +=1
        print ans 
    
    def valid(c1,c2):
        squares=[(0,1),(0,4),(0,6),(1,6),(2,5),(3,6),(4,6),(8,1)]
        return all(x in c1 and y in c2 or x in c2 and y in c1 for x,y in squares)
    
    t0 = time.time()
    run() 
    t1 = time.time()
    print "running time=",(t1-t0),"s"

    # 1217
    # running time= 0.0620000362396 s

  • 相关阅读:
    @Autowired 注解是如何实现的?
    工作 3 年的同事不懂 isEmpty 和 isBlank 的区别,我真是醉了。。
    4 个单词,谷歌返回 16 个 SQL 注入漏洞...
    MySQL 更新不成功,事务问题搞清楚了吗?
    Python Web应用如何部署?
    mapbox加载postgis矢量切片
    tile2lon:地图瓦片编号与经纬度的换算关系
    shp2pgsql:将shapefile导入postgis数据库
    postgreSQL表添加ID自增列
    postgreSQL连接配置
  • 原文地址:https://www.cnblogs.com/theskulls/p/5005040.html
Copyright © 2011-2022 走看看