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?

Paizaの練習問題で「高さ H・幅 W の箱を 2×2 のタイルでピッタリ敷き詰められるか?」に挑戦した!
0 の時を考えてなくてミスった…。
問題概要

高さH、幅Wの箱を、2×2 のタイルで隙間なく敷き詰められるかを判定する。


入力例:

6 2

出力例:

YES




❌NG例

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

rl.once('line', (input) => {
    const [H, W] = input.split(' ').map(Number);

    // 高さと幅が「条件を満たしていれば大丈夫」と思い込んだ
    if(H % 2 === 0 && W % 2 === 0){
        console.log('YES');
    } else {
        console.log('NO');
    }

    rl.close();
});

👉 この書き方だと、高さや幅が 0 でも条件を満たしてしまうので、タイルが置けないのに「YES」って出力しちゃう!



✅OK例

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

rl.once('line', (input) => {
    const [H, W] = input.split(' ').map(Number);

    // 高さか幅が0なら、タイルは置けない
    if(H === 0 || W === 0){
        console.log('NO');
        return;
    }

    // 高さも幅も2の倍数なら、タイルをピッタリ敷き詰められる
    if(H % 2 === 0 && W % 2 === 0){
        console.log('YES');
    } else {
        console.log('NO');
    }

    rl.close();
});



✏️気づきメモ・まとめ

  • 大事なのは「2の倍数」かどうか。タイルのサイズが 2 だから、その単位でピッタリ合わないとスキマが生まれちゃう。

  • 高さや幅が 0 でも「2の倍数判定」には引っかかるから、まず0かどうかを先にチェックして「NO」にするのがポイント。




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

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?