けんちょんの競プロ精進記録

競プロの精進記録や小ネタを書いていきます

AtCoder ABC 085 C - Otoshidama (5Q, 茶色, 300 点)

古き良き、ABS にも入れた代表的問題。

問題概要

10000 円、5000 円、1000 円が合計  N 枚ある。

このとき、これらの合計金額が  Y 円になることがありうるかどうかを判定し、ありうるならばそれを 1 つ答えよ。

制約

  •  1 \le N \le 2000

解法

次の記事に解法を書いている。

qiita.com

コード

#include <iostream>
using namespace std;

int main() {
    int N, Y;
    cin >> N >> Y;
    int res10000 = -1, res5000 = -1, res1000 = -1;
    for (int a = 0; a <= N; ++a) {  // 10000円の枚数を 0 〜 N で調べる
        for (int b = 0; b + a <= N; ++b) {  // 5000円の枚数を 0 〜 N-a で調べる
            int c = N - a - b;  // 1000円の枚数は決まる
            int total = 10000*a + 5000*b + 1000*c;
            if (total == Y) {  // 答えが見つかったら
                res10000 = a;
                res5000 = b;
                res1000 = c;
            }
        }
    }

    // 答えを出力 (見つかっていなくても -1 -1 -1 になるので OK です)
    cout << res10000 << " " << res5000 << " " << res1000 << endl;
}