Loading...
Loading...
Friends on a trip log who paid for what. At the end, the total is split equally to the paisa; when it does not divide exactly, the first friends in the list pay one paisa more each. Print who gets money back and who owes.
Write a class Money holding a whole number of paise, with constructors Money(long paise) and Money(long rupees, long paise) (which calls the first) and String text(). Write a class Friend whose constructor Friend(String name) also starts its paid field at new Money(0).
NAME amount, where the amount is like 120, 50.5 or 0.05.TOTAL Rs r.pp. Then one line per friend in input order: name paid Rs a share Rs b followed by GETS Rs c, OWES Rs c or SETTLED.Example
Input
3 Asha Ravi Meena 2 Asha 100 Ravi 50.5
Output
TOTAL Rs 150.50 Asha paid Rs 100.00 share Rs 50.17 GETS Rs 49.83 Ravi paid Rs 50.50 share Rs 50.17 GETS Rs 0.33 Meena paid Rs 0.00 share Rs 50.16 OWES Rs 50.16
The total 150.50 is 15050 paise: 5017, 5017 and 5016.
Try the problem first. Open one hint at a time.
Store money as a whole number of paise; only text() turns it into rupees and paise.
Split the amount at the .: one digit after the point is tens of paise, so 50.5 is 50 rupees and 50 paise.
Share = total / n, and the first total % n friends get one paisa more.
Without paid = new Money(0) in the constructor, the field is null and the first payment crashes.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor