0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

整数と文字列を半角スペース区切りで入力 1 JavaScript編(paizaランク C 相当)

0
Last updated at Posted at 2026-08-17

【JavaScript】整数と文字列が混ざった入力を位置で判定して処理する

paizaの「整数と文字列を半角スペース区切りで入力 1」という問題に取り組みました。

今回は、数字・演算子・x=が混ざった一次方程式を受け取り、整数には1を足し、文字はそのまま出力する問題です。

問題

入力は、次の形式で1行に与えられます。

a op b e c

各要素には、次の値が入ります。

  • abc:自然数またはx
  • op+または-
  • e=

3つの項のうち、1つだけがxになります。

整数には1を足し、文字はそのまま改行区切りで出力します。

入力例1

1 + 2 = x

出力例1

2
+
3
=
x

入力例2

67 - x = 24

出力例2

68
-
x
=
25

解答コード

process.stdin.resume();
process.stdin.setEncoding("utf8");

var lines = [];

var reader = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

reader.on("line", (line) => {
  lines.push(line);
});

reader.on("close", () => {
  const input = lines[0].split(" ");

  if (input[0] === "x") {
    console.log(input[0]);
  } else {
    console.log(Number(input[0]) + 1);
  }

  console.log(input[1]);

  if (input[2] === "x") {
    console.log(input[2]);
  } else {
    console.log(Number(input[2]) + 1);
  }

  console.log(input[3]);

  if (input[4] === "x") {
    console.log(input[4]);
  } else {
    console.log(Number(input[4]) + 1);
  }
});

入力を半角スペースで分ける

入力が次の場合を考えます。

1 + 2 = x

次のコードで、半角スペースごとに分割します。

const input = lines[0].split(" ");

分割後は、次の配列になります。

["1", "+", "2", "=", "x"]

それぞれの位置には、次の値が入ります。

input[0]; // 自然数 または "x"
input[1]; // "+" または "-"
input[2]; // 自然数 または "x"
input[3]; // "="
input[4]; // 自然数 または "x"

今回のポイントは、入力される値の位置が最初から決まっていることです。

数字またはxが入る位置

数字またはxが入るのは、次の3か所です。

input[0];
input[2];
input[4];

そのため、この3か所だけxかどうかを確認します。

if (input[0] === "x") {
  console.log(input[0]);
} else {
  console.log(Number(input[0]) + 1);
}

input[0]"x"なら、そのまま出力します。

console.log(input[0]);

"x"ではなければ数字なので、Number()で数値に変換してから1を足します。

console.log(Number(input[0]) + 1);

同じ処理を、input[2]input[4]にも行います。

if (input[2] === "x") {
  console.log(input[2]);
} else {
  console.log(Number(input[2]) + 1);
}
if (input[4] === "x") {
  console.log(input[4]);
} else {
  console.log(Number(input[4]) + 1);
}

演算子とイコールはそのまま出力する

input[1]には、必ず+または-が入ります。

input[1]; // "+" または "-"

input[3]には、必ず=が入ります。

input[3]; // "="

どちらも計算する必要がないため、そのまま出力します。

console.log(input[1]);
console.log(input[3]);

入力例1の処理

入力が次の場合、

1 + 2 = x

配列の中身は次のようになります。

["1", "+", "2", "=", "x"]

それぞれを順番に処理します。

input[0] → "1"なので、数値に変換して1を足す
input[1] → "+"なので、そのまま出力
input[2] → "2"なので、数値に変換して1を足す
input[3] → "="なので、そのまま出力
input[4] → "x"なので、そのまま出力

出力結果は次のとおりです。

2
+
3
=
x

入力例2の処理

入力が次の場合、

67 - x = 24

配列の中身は次のようになります。

["67", "-", "x", "=", "24"]

それぞれを順番に処理します。

input[0] → "67"なので68
input[1] → "-"なのでそのまま
input[2] → "x"なのでそのまま
input[3] → "="なのでそのまま
input[4] → "24"なので25

出力結果は次のとおりです。

68
-
x
=
25

なぜ位置で判定するのか

今回の入力形式は、必ず次の順番です。

項 演算子 項 = 項

そのため、演算子や=まで数字かどうか調べる必要はありません。

input[1]; // 必ず演算子
input[3]; // 必ず "="

数字またはxが入る場所だけを判定すればよいので、処理が分かりやすくなります。

input[0];
input[2];
input[4];

isNaN()を使わなかった理由

数字かどうかを調べる方法として、isNaN()があります。

しかし、今回の問題では+-が入力に含まれます。

たとえば、次の処理では、

Number("+");

結果が0になります。

0

そのため、+を数字ではない文字として正しく判定できません。

isNaN(Number("+")); // false

今回のように入力位置が固定されている場合は、isNaN()を使うよりも、位置を使って処理するほうが安全で分かりやすいです。

別の書き方:for文を使う

入力位置を利用しながら、for文でまとめて処理することもできます。

process.stdin.resume();
process.stdin.setEncoding("utf8");

var lines = [];

var reader = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

reader.on("line", (line) => {
  lines.push(line);
});

reader.on("close", () => {
  const input = lines[0].split(" ");

  for (let i = 0; i < input.length; i++) {
    if (i === 1 || i === 3) {
      console.log(input[i]);
    } else if (input[i] === "x") {
      console.log(input[i]);
    } else {
      console.log(Number(input[i]) + 1);
    }
  }
});

i1または3の場合は、演算子または=なので、そのまま出力します。

if (i === 1 || i === 3) {
  console.log(input[i]);
}

それ以外の位置では、xかどうかを確認します。

else if (input[i] === "x") {
  console.log(input[i]);
}

xでもなければ数字なので、1を足します。

else {
  console.log(Number(input[i]) + 1);
}

こちらは短く書けますが、最初のコードのほうが、どの位置を処理しているか確認しやすいです。

初心者にはどちらがよいか

今回の問題では、最初のコードが分かりやすいと思います。

if (input[0] === "x") {
  console.log(input[0]);
} else {
  console.log(Number(input[0]) + 1);
}

少し長くなりますが、次のことを1つずつ確認できます。

  1. xかどうかを確認する
  2. xならそのまま出力する
  3. 数字なら数値に変換する
  4. 1を足して出力する

慣れてきたら、for文を使う書き方にすると、同じ処理をまとめられます。

まとめ

今回のポイントは、入力される値の位置が決まっていることです。

input[0]; // 自然数 または "x"
input[1]; // "+" または "-"
input[2]; // 自然数 または "x"
input[3]; // "="
input[4]; // 自然数 または "x"

数字またはxが入る位置では、xかどうかを確認します。

if (input[0] === "x") {
  console.log(input[0]);
} else {
  console.log(Number(input[0]) + 1);
}

演算子と=は、位置が決まっているため、そのまま出力します。

console.log(input[1]);
console.log(input[3]);

数字と文字が混ざった入力でも、入力形式を確認し、どの位置に何が入るかを整理すると処理を考えやすくなりました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?