「网易官方」极客战记(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
 
本攻略发于极客战记官方教学栏目,原文地址为:
原文地址:https://www.cnblogs.com/codecombat/p/13328071.html