如何在 Hackerrank 和 Hackerearth 中使用 Javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43528075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:59:39  来源:igfitidea点击:

How to Use Javascript in Hackerrank and Hackerearth?

javascriptnode.js

提问by Kannan T

Hi am a newbie to competitive programming the only language i know is Javascript but if i select javascript option i couldn't even understand how to get input and how to print output in both the sites for some problems is Hackerrank the code looks like this

嗨,我是竞争性编程的新手,我知道的唯一语言是 Javascript,但是如果我选择 javascript 选项,我什至无法理解如何在两个站点中获取输入以及如何打印输出以解决某些问题是 Hackerrank 代码如下所示

function processData(input) {
//Enter your code here
} 
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
  _input += input;
});

process.stdin.on("end", function () {
  processData(_input);
});

And in the same hackerrank for some problems the initial code looks like this

对于某些问题,在相同的hackerrank 中,初始代码如下所示

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
 input_stdin += data;
});

process.stdin.on('end', function () {
  input_stdin_array = input_stdin.split("\n");
 main();    
});

function readLine() {
  return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
  var n = parseInt(readLine());
}

Whereas in hackerearth the initial code look like this

而在hackerearth中,初始代码看起来像这样

   function main(input) {
        //Enter your code here
        process.stdout.write("Hello World!");
    }

    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);
    });

If someone give me an example of a program how to get the inputs and print output in those sites or any solved program of those sites using javascript also will do i guess.

如果有人给我一个程序示例,说明如何在这些站点或使用 javascript 的这些站点的任何已解决程序中获取输入和打印输出,我猜也可以。

回答by Pankaj Shukla

Let's take a simple example from HackerEarth: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/

让我们从 HackerEarth 举一个简单的例子:https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/

To provide the solution, you need to do this:

要提供解决方案,您需要执行以下操作:

function main(input) {
    //Enter your code here
    var num = parseInt(input, 10);//This line expects input to be a string so convert to an int as per problem
    var res=1;
    for(var i=num;i>1;i--) {
        res *= i; 
    }
    process.stdout.write(res);//This is how you write output.
} 

EDIT:

编辑:

Here is how you could do it in hackerrank:

以下是您如何在hackerrank中做到这一点:

function main() {
    var n = parseInt(readLine());
    var strN = n.toString();//<-- Convert int n to string
    for(var i=1;i<=10;i++) {
        process.stdout.write(strN+" x "+i+" = "+n*i);//<-- formatting the 
                                                     //question requires
        process.stdout.write("\n");//<-- newline
    }
}

The difference seems to be that in HackerRank, you need to convert the output to string yourself. Hope it helps!

区别似乎是在 HackerRank 中,您需要自己将输出转换为字符串。希望能帮助到你!

EDIT2:

编辑2

For multiline input like:

对于多行输入,如:

5 1
1 2 3 4 1

You can do this:

你可以这样做:

function main(input) {
    //Enter your code here
    var data = input.split('\n');
    var firstLine = data[0].split(' ');
    var len = firstLine[0];
    //process.stdout.write('length:'+len);
    var toFind = firstLine[1];
    //process.stdout.write('toFind:'+toFind);
    //process.stdout.write('\n');
    var arr = data[1].split(' '); 
    //process.stdout.write(arr);
    for(var i=len-1;i>=0;i--) {
        if(arr[i] == toFind){
            process.stdout.write(i+1);
            return;
        }
    }
    process.stdout.write(-1);
}

Notice that input is multi-line, so first you need to split it into lines by doing var data = input.split('\n');. Each split will give you string with spaces in between. So, to get individual characters, you have to split again but this time with space like var firstLine = data[0].split(' ');. Once you have all the input, you are left with writing your own algorithm. Notice that I have left comments too so that you know how to debug in the editor itself.

请注意,输入是多行的,因此首先您需要通过执行将其拆分为多行var data = input.split('\n');。每次拆分都会为您提供中间有空格的字符串。因此,要获得单个字符,您必须再次拆分,但这次使用var firstLine = data[0].split(' ');. 获得所有输入后,您就可以编写自己的算法了。请注意,我也留下了评论,以便您知道如何在编辑器本身中进行调试。

By the way this solution also works and is an accepted solution.

顺便说一下,此解决方案也有效并且是公认的解决方案。

Hope this helps too!

希望这也有帮助!