Algorithmic problem: the number of cases where you have 3500 won of money and you can buy it at the supermarket without leaving any money behind

Asked 2 years ago, Updated 2 years ago, 39 views

I have 3500 won of money, and I have to take this money and get the number of cases that I can buy at the supermarket without leaving any money behind.

The menu includes cream bread, shrimp crackers, and coke, and the prices of each menu are 500 won, 700 won, and 400 won.

However, you have to purchase at least one menu for each. I have no idea how to solve this problem.

If you can answer, it would be really helpful to understand if you could comment briefly on each line. If you reply, I'm going to use the code to correct the problem and study again.

The writing language is Python.

python algorithm

2022-09-20 21:57

1 Answers

sum = 3500
a = 500
b = 700
c = 400

a_max = int(sum / a) + 1
b_max = int(sum / b) + 1
c_max = int(sum / c) + 1

for i in range(0, a_max):
    for j in range(0, b_max):
        for k in range(0, c_max):
            tmp = i * a + j * b + k * c
            if (tmp == 3500) and (i > 0) and (j > 0) and (k > 0):
                print(i, j, k)

You can completely search for the number of cases.


2022-09-20 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.