29
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptで標準入力を扱う方法

Last updated at Posted at 2018-06-26

JavaScriptではNode.jsを使うことで標準入力を扱うことができる。
##例として簡単なAOJの問題を解いてみる

AIZU ONLINE JUDGE

たて a cm よこ b cm の長方形の面積と周の長さを求めるプログラムを作成して下さい。

input

a と b が1つの空白で区切られて与えられます。

output

面積と周の長さを1つの空白で区切って1行に出力して下さい。

Sample input

3 5

Sample output

15 16

##実際に解いてみる

main.js
const main = (standardInput) => {
    const lengthList = standardInput.split(' ');
    const varticalLength = Number.parseInt(lengthList[0]);
    const horizonLength = Number.parseInt(lengthList[1]);

    console.log(`${varticalLength * horizonLength} ${2 * (varticalLength + horizonLength)}`);
}

main(require('fs').readFileSync('/dev/stdin', 'UTF-8'));

ポイントは/dev/stdinを使うこと。

##まとめ
Node.jsを使うことでJavaScriptで標準入力と標準出力が扱えるようになりました。以上。

29
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?