依次生成“5,3,1,3,5”的方程式是什么?

What is the equation to generate `5,3,1,3,5` in sequence?

昨天,我为了获得训练营的奖学金做了一个伪代码测试,1 小时 5 道题,5 道题中有 2 道没答对。我无法使用 数学方程式.

// === === === question 3
// Output:
// *****
//   ***
//     *
//   ***
// *****

// === === === question 5
// Output:
// *   *
//  * *
//   *
//  * *
// *   *

tl:dr

这是 JavaScript 中的答案,不使用数学方程式。我不是数学家,也不是软件工程师。我实际上是软件工程的新手,如果您有更直接的答案,请告诉我。

I assume the output as blocks / air + asterisk.

question 3

// v2 air + asterisk

function cv2() {
    for (i=5; i > 0; i--) {
        // i = [5,3,1]
        if (i %2 == 1) {
            var air = ' '.repeat(5-i);
            var asterisk = '*'.repeat(i);
            console.log(air+asterisk);
        }
    }
    for (i=3; i <= 5; i++) {
        // i = [3,5];
        if (i %2 == 1) {
            var air = ' '.repeat(5-i);
            var asterisk = '*'.repeat(i);
            console.log(air+asterisk);
        }
    }
}

cv2();

question 5

// v1

function ev1() {
    var i = 0;
    var j = 4;
    for (row = 0; row < 5; row++) {
        var result = '';
        for (col = 0; col < 5; col++) {
            if (row == i && col == j) {
                result += '*';
                i++;
                j--;
            } else if (row == col) {
                result += '*';
            } else {
                result += ' ';
            }
        }
        console.log(result);
    }
}

//ev1()

answer with equation

// question 3
// using equation provided by @lukeg in Whosebug.
// v4
// x = [0,1,2,3,4]
// asterisk f(x) = |-4 + 2 * x | + 1
// air f(x) = | | -4 + 2 * x | - 4 |

function cv4() {
    for (x = 0; x < 5; x++) {
        var air = Math.abs(Math.abs(-4 + 2 * x) - 4);
        var asterisk = Math.abs(-4 + 2 * x) + 1;
        console.log(' '.repeat(air) + '*'.repeat(asterisk));
    }
}

//cv4();

// v5 == failed
// asterisk f(x) = 2(x)^3 / 3 - 2(x)^2 - 2x/3 + 5

function cv5() {
    for (x = 0; x < 5; x++) {
        var asterisk = 2 * Math.pow(x,3) / 3 - 2 * Math.pow(x,2) - 2 * x / 3 + 5;
        console.log('*'.repeat(asterisk));
    }
}

//cv5()

// v6 == failed
// x = [0,1,2,3,4]
// y = [5,3,1,3,5]
// asterisk f(x) = -(x)^4 / 3 + 8(x)^3 / 3 - 17(x)^2 / 3 + 4x/3 + 5

function cv6() {
    for (x = 0; x < 5; x++) {
        var asterisk = -Math.pow(x,4) / 3 + 8 * Math.pow(x,3) / 3 - 17 * Math.pow(x,2) / 3 + 4 * x / 3 + 5;
        console.log('*'.repeat(asterisk));
    }
}

//cv6()

// v7
// equation in vertex form
// y = a | x - h | + k , vertex (h, k), another point on the right of the vertex (x2, y2);
// a = y2 - k / x2 - h
// asterisk f(x) = 2 | x - 2 | + 1 
// air f(x) = -2 | x - 2 | + 4
// x = [0,1,2,3,4] y = [5,3,1,3,5]

function cv7() {
    for (x = 0; x < 5; x++) {
        var air =  -2 * Math.abs(x - 2) + 4;
        var asterisk = 2 * Math.abs(x - 2) + 1;
        console.log(' '.repeat(air) + '*'.repeat(asterisk));
    }
}

cv7();

编辑:将 in pure math 更改为 using equation 编辑:添加 lukeg 方程,检查函数 cv4()。 编辑:尝试使用 Raymond Chen 的技术来解决,检查函数 cv5() 和 cv6()。到目前为止,失败了。

您使用的数组的值为 5、3、1、3 和 5。

这很简单,可以很容易地适应任何类似的模式。

如果我理解了这个问题,您是在寻找生成序列 5,3,1,3,5 的简明公式吗? f(x) = |-4 + 2 * x| + 1, for x in (0,1,2,3,4) 是否满足您的需求?

作为旁注,我认为该任务与编程没有太大关系,更多的是 'aha' 时刻。