筆記、LIOJ


Posted by s103071049 on 2021-05-10

| 線上解題系統

類型

  • 實作 function : 函式的參數為輸入,return 為輸出
  • 檔案 I/O : 需要從標準輸入去讀取它的輸入,再從 console.log 去輸出。例如 : LIOJ。

js如何讀取輸入

// readline 的 library 用法
var readline = require('readline');

var rl =readline.createInterface({
    input : process.stdin
})

var lines=[]
// 當使用者輸入完某行資料,我就可以拿到那行資料
rl.on('line',function(line){
    lines.push(line)

})

// EOF function
rl.on('close',function(){
    solve(lines)
})

// 拿到所有資料
function solve(lines){

}

如何測試

  • 方式一、crtl+d (windows 無法)

  • 方式二、呼叫 function

    function solve(lines) {
    var tmp = lines[0].split(' ')
    console.log(Number(tmp[0]) + Number(tmp[1]))
    }
    solve(['4 5'])
    
  • 方式二、新增檔案放輸入,利用 cml 方式產生輸出
  1. touch input.txt
  2. cat input.txt | node code.js
  3. stdin is not a tty,於 git bash 輸入 bash









Related Posts

JavaScript 中的同步與非同步(上):先成為 callback 大師吧!

JavaScript 中的同步與非同步(上):先成為 callback 大師吧!

金魚系列,盒模型篇

金魚系列,盒模型篇

3. Migrations

3. Migrations


Comments