卡码网链接:
多行输入——常用模板
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
while(line = await readline()){
// Write your code here
}
}()
以下题解都省略模板头部分:
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
以下提供卡码网的题解:
1.A+B问题I
void async function () {
while(line = await readline()){
let tokens = line.split(' ');
let a = parseInt(tokens[0]);
let b = parseInt(tokens[1]);
console.log(a + b);
}
}()
2. A+B问题II
思路:没啥别的,经过N行跳过循环就行。
void async function () {
while(line = await readline()){
let tokens = line.split(' ').map(Number);
if(tokens.length === 1){
continue;
}
else{
let tokens = line.split(' ');
let a = parseInt(tokens[0]);
let b = parseInt(tokens[1]);
console.log(a + b);
}
}
}()
3. A+B问题III
思路:校验0行跳过就行
void async function () {
while (line = await readline()) {
let tokens = line.split(' ').map(Number);
if (tokens[0] && tokens[1] != 0) {
let a = parseInt(tokens[0]);
let b = parseInt(tokens[1]);
console.log(a + b);
}
}
}()
4. A+B问题IV
思路:跳过0行且跳过第一个元素循环该行即可
void async function () {
while(line = await readline()){
let tokens = line.split(' ').map(Number);
let sum = 0;
if(tokens[0] != 0){
for(i = 1; i<tokens.length; i++){
sum += tokens[i];
}
console.log(sum);
}
}
}()
5. A+B问题VII
思路:多写一个console.log()输出空行即可
void async function () {
while(line = await readline()){
let tokens = line.split(' ');
let a = parseInt(tokens[0]);
let b = parseInt(tokens[1]);
console.log(a + b);
console.log();
}
}()
6. A+B问题VIII
思路:也不难,比较综合。主要是判断每一组的最后一行元素的位置。增加一个计数器count记录循环了几行即可。如果是最后一行就不要输出空格。
void async function () {
let N = 0;
let count = 0;
while(line = await readline()){
let tokens = line.split(' ').map(Number);
let sum = 0;
if (tokens.length === 1){
N = tokens[0];
count = 0;
continue;
}
else {
for(i = 1;i<tokens.length;i++){
sum += tokens[i];
}
count++;
if(N === count) {console.log(sum);}
else {
console.log(sum);
console.log();
}
}
}
}()
7. 平均绩点
思路:比较简单。主要是复习js中的方法
isNaN(); ——判断是否为NaN值
toFixed(); ——保留两位小数
void async function () {
const obj = {'A':4,'B':3,'C':2,'D':1,'F':0};
while(line = await readline()){
let grade = line.split(' ');
let score = 0;
for(i = 0; i<grade.length; i++){
score += obj[grade[i]];
}
const avg = score / grade.length;
if(isNaN(avg)) {console.log("Unknown")}
else {console.log(avg.toFixed(2))}
}
}()
8. 摆平积木
21.构造二叉树
void async function () {
while(line = await readline()){
let tokens = line.split(' ');
let str1 = tokens[0]
let str2 = tokens[1]
function treeNode(pre, mid){
if (pre.length === 0 || mid.length === 0) {
return [];
}
const rootvalue = pre[0];
const rootIndex = mid.indexOf(rootvalue);
const leftmid = mid.slice(0,rootIndex);
const rightmid = mid.slice(rootIndex + 1);
const leftPre = pre.slice(1,1+leftmid.length);
const rightPre = pre.slice(1+leftmid.length);
const leftTree = treeNode(leftPre,leftmid);
const rightTree = treeNode(rightPre,rightmid);
return leftTree.concat(rightTree, rootvalue);
}
const post = treeNode(str1,str2);
console.log(post.join(''))
}
}()