LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

プログラミング問題集解答例(問5)

Posted at

問5

import math
def question5(tot):
    V = [500, 100, 50, 10, 5, 1]
    C = [10, 10, 10, 10, 10, 10]

    ans = 0
    for v, c in zip(V, C):
        t = min(math.floor(tot / v), c)
        tot -= t * v
        print(v, "円硬貨", t, "枚")
        ans += t
        if t < 0:
            return False

    print("残額 ", tot)

    if tot == 0:
        return ans
    else:
        return False
x = 1672
question5(x)
500 円硬貨 3 枚
100 円硬貨 1 枚
50 円硬貨 1 枚
10 円硬貨 2 枚
5 円硬貨 0 枚
1 円硬貨 2 枚
残額  0





9
x = 5549
question5(x)
500 円硬貨 10 枚
100 円硬貨 5 枚
50 円硬貨 0 枚
10 円硬貨 4 枚
5 円硬貨 1 枚
1 円硬貨 4 枚
残額  0





24
x = 3067
question5(x)
500 円硬貨 6 枚
100 円硬貨 0 枚
50 円硬貨 1 枚
10 円硬貨 1 枚
5 円硬貨 1 枚
1 円硬貨 2 枚
残額  0





11
x = 8802
question5(x)
500 円硬貨 10 枚
100 円硬貨 10 枚
50 円硬貨 10 枚
10 円硬貨 10 枚
5 円硬貨 10 枚
1 円硬貨 10 枚
残額  2142





False
x = -124
question5(x)
500 円硬貨 -1 枚





False
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