【问题标题】:Need an Algorithm/Logic To Step Through Steps and Assign Variables, Pseudocode is fine需要一个算法/逻辑来逐步执行步骤并分配变量,伪代码很好
【发布时间】:2021-09-22 19:57:30
【问题描述】:

需要帮助为以下问题提出一种有效的算法。

我有几个变量。

布尔变量 S = 开始步骤序列 - 真/假

整数变量 R = 配方总数 - 常数 50

整数变量 X = 配方步骤数 - 可以是 1 到(变量 R)

整数变量 Y = 起始配方步骤 - 可以是 1 到(变量 R)

整数变量 Z = 结束配方步骤 - 可以是 1 到(变量 R)

整数变量 C = 当前配方步骤 - 可以是 1 到(变量 R)

整数变量 T = 持续时间 - 恒定 60 秒

布尔变量 D = 已完成的步骤 - 真/假

所以我正在解决最终用户可以更改变量 X 和 Y 的问题。

至少我需要一种算法,它可以查看步数(变量 X),然后获取起始步(变量 Y)的输入,然后计算结束步(变量 Z)。如果这些步骤导致变量 Z 超过 50(变量 R),则从 1 开始并相应地增加值。

一旦开始步骤序列(变量 S)设置为 True,那么算法将需要从变量 Y 开始并将其分配给当前配方步骤(变量 C),然后每个持续时间(变量 T)它需要增加变量C 上升 1 到下一步。如果由于导致值翻转的步骤数,结束步骤(变量 Z)小于起始步骤(变量 Y),那么变量 C 需要发生同样的事情,并且一旦在值为 50(变量 R)并且需要更多增量,它将翻转到 1 并继续递增,直到完成步数,这意味着变量 C 等于变量 Z,并且最后一步的持续时间已过期。然后,一旦最后一步持续时间完成,将 Steps Done (Variable D) 标记为 True,并将变量 S 设置为 flase。

【问题讨论】:

  • “如果这些步骤导致变量 Z 超过 50(变量 R)......”那么如果 Z 超过 X(步数)或超过 Y(起始步骤),是否可以?我要问的是:如果结束步骤大于配方中的步骤数,可以吗?
  • 另外,程序是否需要每隔 xx 毫秒检查一次 S 是否为真,或者如果它是假的,它只需要停止运行,而不是每隔几毫秒自动检查一次?

标签: algorithm logic pseudocode


【解决方案1】:

我在 JavaScript 中制作了算法,因为它可以作为 sn-p 在您的浏览器中运行,这对于测试此答案中的代码输出很有用。

此外,为了清楚起见,我声明了许多名称较长的变量(即current_step = c)以及单字母名称。我知道这会使代码比必要的长得多,但提高了可读性。

这里是 sn-p:

let s = true; // Whether to start sequence
const r = 50; // Total number of steps
let x; // Number of recipe steps 1 -> r
let y; // Starting recipe step 1 -> r
let z; // Ending recipe step 1 -> r
let c; // Current recipe step 1 -> r
const t = 60; // Time duration in seconds
let d = false; // Whether steps are done



async function algorithm(options) {
    return new Promise(resolve_entire_algorithm => {
        const total_steps = options.r;
        const number_of_steps = options.x;
        const starting_step = options.y;
        let ending_step = starting_step + number_of_steps;
        while(ending_step > /*Math.min(starting_step,number_of_steps,*/total_steps/*)*/) ending_step -= 50;
        if(!options.s) {
            return {s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:options.c,t:options.t,d:options.d};
        };

        let current_step = starting_step;
        let time_duration = options.t;
        let completed_all_steps = options.d;

        async function complete_step() {
            return new Promise(resolve => {
                if(completed_all_steps) {
                    resolve(true);
                    return;
                };
                if(current_step > number_of_steps && ending_step < starting_step && ending_step < number_of_steps) c = 1;
                if(current_step > total_steps) current_step -= total_steps;
                if(current_step == ending_step) {
                    completed_all_steps = true;
                };
                console.log(current_step, ending_step, completed_all_steps);
                setTimeout(() => {
                    resolve(complete_step);
                    current_step++;
                }, time_duration*10);
            });
        };
        async function listen(func) {
            const result = await func();
            if(result === true) {
                current_step--;
                resolve_entire_algorithm({
                    s:options.s,r:total_steps,x:number_of_steps,y:starting_step,z:ending_step,c:current_step,t:options.t,d:completed_all_steps
                });
                return;
            } else if(typeof func == "function") {
                console.log("Not completed (even if current_step==ending_step, you still have to wait until the current_step has expired. Takes (T) seconds per step, including the last step.)");
                listen(func);
            };
        };
        listen(complete_step);
    });
};


const initial_settings = {s,r,x:30,y:30,z,c,t,d};

algorithm(initial_settings)
.then(output_settings => {
    console.log("\n\n...\nAlgorithm finished.\n\n\nOriginal:\n",initial_settings, "\n\nResult:\n",output_settings);
});

请注意:我没有使用每一步的秒数,而是使用 600 毫秒来加速代码。您可以通过将 time_duration*10 更改为 time_duration*1000 在生产中解决此问题

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2017-11-27
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多