zoukankan      html  css  js  c++  java
  • 「网易官方」极客战记(codecombat)攻略-沙漠-立方雷区-cubic-minefield

    (点击图片进入关卡)

    用数学的强大,找到穿越雷区的路。

    简介

    食人魔的数学很差,所以我们使用数学来计算穿越雷区的通路,这里使用立方方程cubic equation,根据x坐标计算y坐标。

    您可以使用代码来定义 {x, y} 坐标。 你的任务是写 power 函数来计算一个数的幂指数。

    默认代码

    # 穿过雷区

     

    # 这个函数返回乘以次数的数字。
    def mult(number, times):
        total = 0
        while times > 0:
            total += number
            times -= 1
        return total

     

    # 这个函数返回乘方的数字。
    def power(number, exponent):
        total = 1
        # 补全函数。

     

        return total

     

    # 别修改这些代码
    # 你可以在塔上找到方程的系数
    tower = hero.findFriends()[0]
    a = tower.a
    b = tower.b
    c = tower.c
    d = tower.d
    x = hero.pos.x

     

    while True:
        # 用三次方程求路径
        y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0)
        hero.moveXY(x, y)
        x = x + 5

    概览

    乘方(Exponentiation) 是一种涉及两个数字的数学运算。 那两个数字分别叫 base 和 exponent 。当 指数 是正整数时…… 乘方得出底数连续相乘的结果:

    底数 ** 指数 = 底数 * 底数 * ... * 底数
        ( 共有 指数 个 底数 )

    例子里有一个 multiplication 函数。 power 函数也是相似的方法,不过你要用 * 代替 + ,并且( total 的)起始值应该是 1 而不是 0 。

    立方雷区 解法

    # 穿过雷区

     

    # 这个函数返回乘以次数的数字。
    def mult(number, times):
        total = 0
        while times > 0:
            total += number
            times -= 1
        return total

     

    # 这个函数返回乘方的数字。
    def power(number, exponent):
        total = 1
        # 补全函数。
        while exponent>0:
            total *= number
            exponent -=1
        return total

     

    # 别修改这些代码
    # 你可以在塔上找到方程的系数
    tower = hero.findFriends()[0]
    a = tower.a
    b = tower.b
    c = tower.c
    d = tower.d
    x = hero.pos.x

     

    while True:
        # 用三次方程求路径
        y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0)
        hero.moveXY(x, y)
        x = x + 5
     
    本攻略发于极客战记官方教学栏目,原文地址为:
  • 相关阅读:
    codeforces 869E. The Untended Antiquity(二维树状数组,随机化)
    bzoj 3083: 遥远的国度(树上换根操作,树剖+询问整个子树)
    hdu 5534 Partial Tree(dp+降唯,好题)
    AtCoder Regular Contest 075 E
    hihocoder 1387 A Research on "The Hundred Family Surnames"(树,lca,求同一颜色的直径)
    hdu 5458 Stability(生成树,树链剖分,好题)
    推荐一套个人ui组件库
    回望2019,期盼2020
    如何从产品的角度对待自己的博客
    致一名迷茫的我
  • 原文地址:https://www.cnblogs.com/codecombat/p/13328071.html
Copyright © 2011-2022 走看看