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?

毎日増加するお金

Last updated at Posted at 2025-06-08

Paizaの「所持金が10%ずつ増える日数を求める問題」にチャレンジ!
今回のポイントは「小数点以下切り捨て」。


問題概要

  • 所持金 A 円からスタート
  • 毎日 10% 増加(小数点以下切り捨て)
  • 目標金額 B 円を超えるのは何日後?

入力例:
10 15

出力例:

6




❌NG例

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


rl.once('line', (input) => {
    const [A, B] = input.split(' ').map(Number);
    
    
    let count = 0;
                             // ✗ 小数切り捨てしてない!
    for(let temp = A; temp <= B; temp *= 1.1, count++);
    
    console.log(count);

    
    rl.close();
});

❌ 1円未満の増加も加算されるから正解にならない!



✅OK例

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


rl.once('line', (input) => {
    const [A, B] = input.split(' ').map(Number);
    
    
    let count = 0;
    for(let temp = A; temp <= B; temp += Math.floor(temp * 0.1), count++);
    
    console.log(count);

    
    rl.close();
});
  • 増加額 = Math.floor(現在の所持金 * 0.1)
  • 次の日の所持金 = 現在の所持金 + 増加額



✅whileバージョン

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

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

    let money = A;
    let days = 0;

    while (money <= B) {
        // 10%増加分(小数点以下切り捨て)
        let increase = Math.floor(money * 0.1);
        money += increase;
        days++;
    }

    console.log(days);
    rl.close();
});



📝 気づきメモ

  • temp *= 1.1 で進めると小数が残ってアウト!
  • 1円未満の「増加額」は無視する → Math.floor

まとめ

「10%増加問題」、実は小数切り捨てが一番のポイントだった。あとは、前回と同じようなコードでつまらなかった(´;ω;`)

あと、毎日所持金が10%増加するとか、どこのファンドだよ。教えてくれ。




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

0
0
1

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?