Race code network input and output problems (js v8)
I think the documentation is very unclear, and it took me a long time to understand it
For the js v8 engine, the operation is like this
read related
read_line()
Function : read a line of input
Explanation : It will read up to 1024 characters, and if it encounters a carriage return or end character before reaching 1024, it will end early.
Explanation : The easiest way to read multiple lines is while((line = read_line()) != '')
.
gets(n)
Function : read n characters
Explanation : It will read up to n characters, and if it encounters a carriage return or end character when it has not reached n, it will end early.
Note : A carriage return may be included in the return value. ****
readInt()
Function : read a long integer
readDouble()
Function : read a floating point
output related
printsth(sth, …)
Description: output without carriage return
Explanation: Output sth to the console. When there are multiple parameters, separate them with spaces; do not add a carriage return at the end.
console.log(sth, …)
Description : output with carriage return
Explanation : Output sth to the console. When there are multiple parameters, separate them with spaces; add a carriage return at the end.
Well, that's about it
conssole.log()
But we generally use it when we output
After all, those who learn the front end should have been deeply impressed by this API.
Then let's write an example question from Saima.com
Here is to read multiple lines of input, we write in while
the conditional statement
while((line = read_line()) != '')
In this way, multiple lines of input can be read
var line;
let getSum = (m,n) => {
let sum = 0;
while(n) {
sum += m;
m = Math.sqrt(m);
n--;
}
return sum.toFixed(2) ;
}
while((line = read_line()) != ''){
//读取
let arr = line.split(' ');
let m = parseInt(arr[0]);
let n = parseInt(arr[1]);
let sum = getSum(m, n);
console.log(sum);//输出
}
In addition, for only one set of input
For example, the first line of input is the number n, followed by n lines of data .
In this case, you only need to read an n first, and then read the data n times in a loop .
let n = readInt();
let arr = [];
for(let i = 0; i < n; i++)
{
arr[i] = read_line();
}
Most of the written test algorithm questions of Saima.com support the local IDE .
But for our front end, it is very troublesome to input samples on vscode
Even I'm very rusty on this stuff
Now we have a good way to configure vscode, so that our vscode can also conveniently test cases
After using this method, we can directly paste the local tested code to the Saima website without any other modification
There is a very useful npm package readline-sync
Next we install him
Execute the following command on the local ide terminal
npm install readline-sync
Then import the module in the file and rename it
var __readline = require('readline-sync')
__readline.setDefaultOptions({
prompt: ''})
var read_line = __readline.prompt
The method of reading on the Saima website here is read_line
so we also renamed it toread_line
Then we can happily write code on vscode
Let's demonstrate how to write the sample question just now on vscode:
- Create a js file
2. Write code
//导入包
var __readline = require('readline-sync')
__readline.setDefaultOptions({
prompt: ''})
var read_line = __readline.prompt
//下面的代码是我们需要贴到赛码网编译器的
var line;
let getSum = (m,n) => {
let sum = 0;
while(n) {
sum += m;
m = Math.sqrt(m);
n--;
}
return sum.toFixed(2) ;
}
while((line = read_line()) != ''){
let arr = line.split(' ');
let m = parseInt(arr[0]);
let n = parseInt(arr[1]);
let sum = getSum(m, n);
console.log(sum);
}
3. Execute in the terminalnode .\赛码网输入输出测试.js
The above is the node command, which means that there is no more to say about executing a certain file
At this time we can enter the sample
4. Input sample
You can see that the correct result has been output
After the test has no problem, we can put the code into the Saima.com compiler for execution