HackerEarth:如何从 STDIN 读取并写入 STDOUT?

Nas*_*aig 5 javascript stdin stdout

这里有人在 HackerEarth 上解决问题吗?我对他们提供输入数据的方式感到困惑。

迄今为止,我一直在使用 Leetcode 来解决问题,我对它们非常满意,但不幸的是,有些人更喜欢 HackerEarth 来举办编码挑战,而我在尝试正确读取输入测试用例时遇到了问题。

以此为例https://www.hackerearth.com/practice/algorithms/searching/ternary-search/practice-problems/algorithm/small-factorials/submissions/

我做了研究,发现他们的“解决方案指南”有错误的信息:https ://www.hackerearth.com/docs/wiki/developers/solution-guide/

我如何读取各行并在 JS (Node v10) Judge 中输出结果?

谢谢。

Emm*_*mma 3

  • 刚刚登录并在这里查看。

  • 似乎与我不喜欢的 HackerRank 类似。(LeetCode 的 UI 很有趣,而且更容易使用。)

  • 在 LeetCode 上,我们不必打印输出,在这里我们似乎必须打印输出(例如在 JavaScript 中,我们会使用console.log更不用说在方法内部打印通常是一种不好的编码实践)。

该解决方案(从其中一项活动复制)似乎正在通过,您可以根据它来解决问题:


/*
// Sample code to perform I/O:

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
   main(stdin_input);
});

function main(input) {
    process.stdout.write("Hi, " + input + ".\n");       // Writing output to STDOUT
}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/

// Write your code here


process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
    stdin_input += input;
});
process.stdin.on("end", function () {
   main(stdin_input);
});
function main(input) {
    input = input.split('\n');
    input.shift();
    input.forEach(n => {
        n = parseInt(n);
        let fact = BigInt(1);
        while(n){
            fact = BigInt(fact) * BigInt(n);
            n--;
        }
        console.log(String(fact).replace('n',''));
    });
}
Run Code Online (Sandbox Code Playgroud)