Nodejs OJ Online Written test response program (several input processing Methods)

Source: Internet
Author: User

Recently participated in a number of online written examinations. But... I do not learn computer, only JS will not layer C++,java,c (well, have learned, but forget). Terrible is I have not learned nodejs, how to do, how to do. Node is not the use of JS it? So just use the learning standard input and output can solve Problems. The following summarizes several methods, can be used as a problem-solving template, directly set on it. I am not very kind, think I am very kind of please pay attention to me, I like to have more fans.

one, processing Single-line Input

This is one of the most simple inputs. look, I'm going to give you a template:

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout}); rl.on (' Line ',function(data) {//Get input    varInputs = Data.trim (). split ('); //processing    varresult =deal (inputs); //Output ResultsConsole.log (result);});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = '; //dosomething    returnresult;}

To express the most original Author's gratitude and respect, here gives the original Address: http://ankanglee.cc/2016/08/01/NodeJS-OJ%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E6%9D%BF/. Thank you, you let me learn how to handle input correctly.

An example is given below:

Topic:

  

Set of Templates:

  

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout}); rl.on (' Line ',function(data) {//Get input    varInputs =data; //processing    varresult =deal (inputs); //Output ResultsConsole.log (result);});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = Inputs.split (""). reverse (). join (""); //dosomething    returnresult;} Javascript:void(0);

Let's see, It's not right:

  

I rub, the postgraduate examination question how so water ah. But That's not the point, but the point is, do you learn how to handle single-line input? Https://www.nowcoder.com/practice/171278d170c64d998ab342b3b40171bb?tpId=40&tqId=21336&tPage=1&rp=1 &ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking, try not to know?

Processing multiple lines of input (the number of rows in a set has been determined)

The so-called multi-line input is a group of test data have multiple Lines. Let's start with a template and give an example

  

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout});varK = 1;//Enter K-line (here is a few lines in a Group)varInputs =[];rl.on (' Line ',function(data) {//Get inputInputs.push (data.trim ()); if(K = = Inputs.length) {//        //processing        varresult =deal (inputs); //Output ResultsConsole.log (result); //Qing 0Inputs.length = 0; }});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = '; //dosomething    returnresult;}

Topic:

Use a template to do the reading

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout});varK = 2;//Enter K linevarInputs =[];rl.on (' Line ',function(data) {//Get inputInputs.push (data.trim ()); if(K = = Inputs.length) {//        //processing        varresult =deal (inputs); //Output ResultsConsole.log (result); //Qing 0Inputs.length = 0; }});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = []; //dosomething    varS1 = Inputs[0].split (""); varS2 = Inputs[1].split (""); varLen =s1.length;  for(vari = 0;i<len;i++) {result.push (s1[i]); Result.push (s2[len-i-1]); }          returnResult.join ("");}

Results:

I go, is it really so easy to take the exam? well, I admit that I have been looking for a long time to find this problem, the postgraduate examination is Difficult. https://www.nowcoder.com/practice/7f436c901a0d450ebdec1168e3e57cc2?tpId=40&tqId=21533&tPage=10&rp= 5&ru=%2fta%2fkaoyan&qru=%2fta%2fkaoyan%2fquestion-ranking; title address is here, go to test go.

Three or one group of test data rows indeterminate (1+n Mode)

What do you mean? This means that the first line of a set of test data is a number n, followed by n rows, which 1+n a set of test Data. Template to Top:

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout});varInputs = [];varnum = 0; Rl.on (' Line ',function(data) {if(num = = 0) {num=number (data.trim ()); } Else{inputs.push (data.trim ()); if(num = =Inputs.length) {//processing            varresult =deal (inputs); //Output ResultsConsole.log (result); //Qing 0Inputs.length = 0;//cannot be changednum = 0;//cannot be changed        }    }});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = '; //dosomething    returnresult;}

Topic:

Set of templates

varReadLine = require (' readline ')); RL=readline.createinterface ({input:process.stdin, output:process.stdout});varInputs = [];varnum = 0; Rl.on (' Line ',function(data) {if(num = = 0) {num=number (data.trim ()); } Else{inputs.push (data.trim ()); if(num = =Inputs.length) {//processing            varresult =deal (inputs); //Output ResultsConsole.log (result); //Qing 0Inputs.length = 0;//cannot be changednum = 0;//cannot be changed        }    }});/** * [deal description] * @param {[type]} inputs [description] * @return {[type]} [description]*/functiondeal (inputs) {varresult = 0; //dosomething    vardata =Inputs.map (function(item) {returnparseint (item.trim ())}). Sort (function(v1,v2) {returnV1-v2}); varLen =data.length; if(len%2==0){        varL = len/2;    result = ((data[l]+data[l-1])/2). toFixed (2); }Else{        varL = (len-1)/2; result =data[l]}returnresult;}

Are you right? obviously,

haha, It's Fun. Address HTTPS://WWW.NOWCODER.COM/PRACTICE/2364FF2463984F09904170CF6F67F69A?TPID=40&TQID=21367&TPAGE=2&RP =1&ru=%2fta%2fkaoyan&qru=%2fta%2fkaoyan%2fquestion-ranking, you have to experience it.

Iv. Summary

Now a lot of company recruitment to the online test, if you are a front-end developers, and will not java,c++, then you can choose js, the first step to choose JS is that you will process the data, if you do not process data, no matter how smart you, algorithmic learning, JS learn much better, sorry, the written test can Not. I deeply hurt, so decided to study this thing, standing on the shoulders of predecessors summed up the above methods, I hope to help you. If the above content is helpful to you, don't forget to recommend it, if you can focus on it I will be even better. finally, I wish you success!

Reference: http://ankanglee.cc/2016/08/01/NodeJS-OJ%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA%E6%A8%A1%E6%9D%BF/

Nodejs OJ Online Written test response program (several input processing Methods)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.