在acwing上刷了一些语法题巩固下js
输入输出
1. A + B
首先读取时讲所有输入读到缓存buf
然后读完后先把a和b用lamda表达转成int,就可以输出了
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
let [a,b] = buf.split(" ").map(x => {return parseInt(x);});
console.log(a+b);
});
608. 差
多行数据的输入split参数是\n,格式化输出用``
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
let [a,b,c,d] = buf.split("\n").map(x => {return parseInt(x);});
console.log(`DIFERENCA = ${a*b-c*d}`);
});
判断语句
665. 倍数
===既判断值,也判断类型
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
let [a,b] = buf.split(" ").map(x => {return parseInt(x);});
if (a % b === 0 || b % a === 0) console.log("Sao Multiplos");
else console.log("Nao sao Multiplos");
});
循环语句
708. 偶数
for (let i = 2; i <= 100; i+=2) console.log(i);
数组
737. 数组替换
用了split后返回值就是数组,直接用数组接
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
let X = buf.split("\n").map(X => {return parseInt(X);});
for (let i = 0; i < 10; i++) {
if (X[i] <= 0) X[i] = 1;
console.log(`X[${i}] = ${X[i]}`);
}
});
字符串
760. 字符串长度
直接用内置的字符串类型
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on('end', function() {
let X = buf.split("\n")[0];
console.log(X.length);
});
函数
805. x和y的最大值
let buf = '';
process.stdin.on('readable', function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let max = (x,y) => {return x > y? x : y;}
process.stdin.on('end', function() {
let [a,b] = buf.split(" ").map(x => {return parseInt(x);});
console.log(max(a,b));
});