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?

TypeScriptでの配列の扱い方と標準入力の読み込み

Last updated at Posted at 2025-10-09

配列の宣言の仕方
TypeScriptでは、配列を以下のように型付きで宣言します。

let numbers: number[] = [1, 2, 3];
let names: string[] = ["Alice", "Bob", "Charlie"];
let flags: boolean[] = [true, false, true];

各要素を文字列に変換する

const numbers: number[] = [10, 20, 30];
const strings: string[] = numbers.map((n) => n.toString());
console.log(strings); // ["10", "20", "30"]

標準出力について

標準出力

10 1 2
// ファイルを読み込み、行ごとに分割
const input: string[] = fs.readFileSync("./input.txt", "utf8").trim().split("\n");

// 1行目 → N
const N: number = Number(input[0]);

// 2行目 → 配列として格納
const [arr1,arr2]: number[] = input[1].split(" ").map(Number);

Node.js に標準で用意されている ファイル操作用のモジュール(ライブラリ)のことです。
fs は File System(ファイルシステム) の略です。

require("fs") → Node.js の ファイルシステムモジュールを使う

.readFileSync("/dev/stdin", "utf8") → 標準入力を同期的に文字列として読み込む

.trim() → 最後に余分な改行が入っているケースを削除

.split("\n") → 改行ごとに区切って 配列に変換
このように入力ファイルに値があるとき、10という値を出力する方法について、説明します。

tsc main.ts とは?

tsc は TypeScript Compiler(コンパイラ) の略で、
TypeScript で書かれたコードを JavaScript に変換(トランスパイル) するコマンドです。

🧩 TypeScript は直接実行できない

TypeScript(.tsファイル)は、
型注釈など JavaScript にない構文を含んでいます。

🚀 変換したコードを実行

TypeScriptをコンパイルしたあと、
生成された main.js は Node.js で実行できます。

node main.js

mapの使い方

要素を取り出す場合,mapを使用します。

const input: string = fs.readFileSync("./input.txt", "utf8").trim();
const [N,A,B]: number[] = inputs[0].split(" ").map(Number);

fileterの使い方

filter()は条件を満たす要素だけを抽出した新しい配列を作ります。

const input: string = fs.readFileSync("./input.txt", "utf8").trim();
const count = input.split("").filter((c) => c === "1").length;

応用例:各桁の和がA以上B以下の数の合計を求める

次の例では、1以上N以下の整数のうち、各桁の和がA以上B以下のものを合計しています。

// 入力を読み込み
const input: string[] = fs.readFileSync("./input.txt", "utf8").trim().split("\n");
const [N, A, B]: number[] = input[0].split(" ").map(Number);

let total = 0; // 条件を満たす数の合計

for (let i = 1; i <= N; i++) {
  const str: string = i.toString(); // 数字を文字列化
  let sum = 0; // 桁の合計値

  // 各桁を取り出して足す
  for (let j = 0; j < str.length; j++) {
    sum += Number(str[j]);
  }

  // A以上B以下なら合計に加算
  if (sum >= A && sum <= B) {
    total += i;
  }
}

console.log(total);

オブジェクトの変換

次の配列を、id をキーとしたオブジェクトに変換する。

const users = [
  { id: 1, name: "Alice", age: 25 },
  { id: 2, name: "Bob", age: 30 },
  { id: 3, name: "Charlie", age: 28 },
];

const result: { [key: number]: { name: string; age: number } } = {};

for (let i = 0; i < users.length; i++) {
  const u = users[i];
  result[u.id] = { name: u.name, age: u.age };
}

console.log(result);

重複を取り除いて新しい配列を作る

const numbers = [1, 2, 2, 3, 4, 4, 5];

// 出力例
// [1, 2, 3, 4, 5]
const numbers = [1, 2, 2, 3, 4, 4, 5];

const uniqueNumbers = Array.from(new Set(numbers));

console.log(uniqueNumbers);
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?