Write an algorithm for the following problem. The input consist of three integers a, b, and w. Assume that you live in a country that has two types of coins, one worth a dollars and the other b dollars. Both you and the storekeeper have a pocket full of each. You must pay him w dollars. You can give him any number of coins, and he may give you change with any number of coins. Your algorithm must determine whether or not this is possible and, if so, describe some way of doing it (not necessarily the optimal way). [Hint: Compute GCD(a, b), and use the three values g, u, and v. Consider the two cases when g divides w and when it does not. (If you want to find the optimal number of coins, basically you change a solution by using the fact that ( bg ) · a − (ag ) · b = 0.)]
Write an
b, and w. Assume that you live in a country that has two types of coins, one worth
a dollars and the other b dollars. Both you and the storekeeper have a pocket full of
each. You must pay him w dollars. You can give him any number of coins, and he
may give you change with any number of coins. Your algorithm must determine
whether or not this is possible and, if so, describe some way of doing it (not necessarily the optimal way). [Hint: Compute GCD(a, b), and use the three values g, u,
and v. Consider the two cases when g divides w and when it does not. (If you want
to find the optimal number of coins, basically you change a solution by using the
fact that ( bg ) · a − (ag ) · b = 0.)]
Step by step
Solved in 2 steps