【问题标题】:HackerEarth: How to read from STDIN and write to STDOUT?HackerEarth:如何从 STDIN 读取并写入 STDOUT?
【发布时间】:2020-10-11 19:23:00
【问题描述】:

这里有人解决 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)判断中读取各个行并输出结果?

谢谢。

【问题讨论】:

    标签: javascript stdout stdin


    【解决方案1】:
    • 刚刚登录并查看了here

    • 似乎类似于我不喜欢的 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',''));
        });
    }
    

    【讨论】:

    • 谢谢你,艾玛。这有帮助。到目前为止,在我的 JS 旅程中,我从未使用过 BigInt,所以我可能必须为此进行研究。
    • 感谢发帖,这里的链接看起来好像坏了,但这很有帮助!你才是真正的MVP。
    【解决方案2】:

    在访问输入 HackerEarth 与 leetcode 和 Hacker Rank 相比时,您必须按行号从标准输入 (STDIN) 中提取输入,您可以找到更多详细信息 here

    例如,如果输入的格式如下
    输入格式:
    第一行包含整数 N。
    第二行包含字符串 S。

    然后您将通过新行(“\n”)拆分 STDIN 并逐行访问每个输入

    以下是如何在 JavaScript 中访问输入的示例

    var input1 = 0;
    var input2 = "";
    var stdin_input = ""
    process.stdin.on("data", function (input) {
        // Reading input from STDIN
        stdin_input += input;  
        input1 = stdin_input.split("\n")[0] 
        input2 = stdin_input.split("\n")[1]
    
        console.log("input1 = ", input1)
        console.log("input2 = ", input2)
                           
                                 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 2019-11-13
      • 1970-01-01
      • 2011-05-05
      • 2017-08-16
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      相关资源
      最近更新 更多