1
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
Posted at

前回に引き続きpaizaの構造体の問題に挑戦!

早く使いこなせるように練習あるのみ!


問題概要

  • クラスの人数Nと、N人分の「名前・年齢・誕生日・出身地」の情報が渡される

  • 最後に欲しい年齢Kが渡される

  • K歳の生徒の名前を出力する


入力例:

3
mako 13 08/08 nara
megumi 14 11/02 saitama
taisei 16 12/04 nagano
14

出力例:

megumi






✅ OKコード例:

const rl = require('readline').createInterface({ input: process.stdin });

const lines = [];

rl.on('line', (input) => {
  lines.push(input);
});


rl.on('close', () => {
  // 1行目:クラスの人数 N を取得
  const N = Number(lines[0]);

  // N+1 行目:探したい年齢 K を取得
  const K = Number(lines[N + 1]);

  // ✅ クラスを定義
  class Student {
    // constructor は「new Student(...)」されたときに呼ばれる    
    constructor(name, old, birth, state) {
      // this は そのインスタンス自身 を指す
      this.name = name;     // 名前
      this.old = old;       // 年齢
      this.birth = birth;   // 誕生日
      this.state = state;   // 出身地
    }
  }



  // Student インスタンスを入れる配列
  const students = [];

  // 2行目〜N行目のデータを 1人ずつ処理
  for (let i = 1; i <= N; i++) {
    const [name, old, birth, state] = lines[i].split(' ');
    // ✅ new Student()で Student クラスから「インスタンス(実体)」を作る
    students.push(new Student(name, Number(old), birth, state));
  }

  // 年齢が K の生徒を探す
  for (const student of students) {
    if (student.old === K) {
      console.log(student.name);
      break; // 見つけたら終了
    }
  }
});

🔹 1. class Student は「設計図」

名前・年齢・誕生日・出身地を持つ、という型を定義している。



🔹 2. new Student(...) は「インスタンス化」

new を使うと、クラスの設計図から 1 つの実体(オブジェクト)を作る。

この実体を「インスタンス」と呼ぶ。


例:

const s = new Student(...)

sStudent クラスのインスタンス。

👉 new を使った瞬間に、constructor が呼ばれて初期化が行われる。



🔹 3. this は「自分自身のデータ」

constructor の中で this.name = name のように書くことで、作られたそのインスタンスの中に値が保存される。


つまり、newconstructorthis という流れで
設計図(クラス)に値を入れて 1 つのモノ(インスタンス)を完成させる。


作った後は student.old のように . を使って、
そのインスタンスの中身を自由に取り出したり使える。





🗒️メモ

  • new が設計図(class)を実物にするスイッチ。
  • constructor が設計図にデータを流し込む工場。
  • this はそのインスタンス自身を指し、工場の中でデータを詰め込む箱のような役割。




僕の失敗談(´;ω;`)と解決法🐈






✅ OKコード例:コメントなし

const rl = require('readline').createInterface({input:process.stdin});

const lines = [];

rl.on('line', (input) => {
    lines.push(input);
});


rl.on('close', () => {
    const N = Number(lines[0])
    const K = Number(lines[N+1]);
    
    class Student{
        constructor(name, old, birth, state){
            this.name = name;
            this.old = old;
            this.birth = birth;
            this.state = state;
        }
    }
    
    const students = [];
    for(let i = 1; i <= N; i++){
        const [name, old, birth, state] = lines[i].split(' ');
        students.push(new Student(name, Number(old), birth, state));
    }
    
    for(const student of students){
        if(student.old === K){
            console.log(student.name);
            break;
        }
    }
});
1
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
1
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?