144 - Student Grants


Difficulty : easy

Solution Description :

Ad hoc simulation

Read this line carefully
"When the machine is switched on in the morning, with an empty output store, it immediately moves 1 coin into the output store. When that has been dispensed it will then move 2 coins, then 3, and so on until it reaches some preset limit k. It then recycles back to 1, then 2 and so on."

1. When output store empty then the machine move coin. At part move 1 coin, then 2 coins, then 3 coins, and so on. If moving coin exit the limit k then the machine move 1 coin, then 2 coins, and so on. (Remember that the machine move coin when output store is empty).
2. A student withdraw coin role:  If x coins in output store, a student need y coin
   a. If x==y then the student withdraw y coins and  leave the student. Now output store is empty.
   b. if x>y then the student withdraw y coins and leave the student. Now (x-y) coin in output store.
   c. if x<y then the student withdraw y coins and the student  rejoins the queue at the end.

Just simulate the process using a queue of objects that hold a student number and an amount of money remaining.