Geometric Inflation
Time limit1sMemory limit128 MB
Simulate the monthly price recurrence with half-up cent rounding and print the Nth month cost with comma grouping.
- Level
Easy2 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
This is a very unusual model of inflation. The cost of an item this month is the cost three months ago multiplied by the cost two months ago, divided by the cost one month ago.
Costs are given in dollars, and the digits after the decimal point are cents, so there are never more than two of them. No cost can include a partial cent, so round the result for every month to the nearest whole cent. A result that lands exactly halfway between two cents rounds up. Rounding happens once per month, after the division. The product is not rounded before it is divided.
Suppose dumplings from the corner shop cost $5.23 in month 1, $5.50 in month 2, and $5.52 in month 3. The cost in month 4 is 5.23 multiplied by 5.50, which is 28.765, divided by 5.52, which is 5.2110..., and that rounds to $5.21.
Input
The input has several lines. Each line starts with three decimal numbers, the costs in month 1, month 2, and month 3. All three costs are positive and are written with one or two digits after the decimal point. The line then holds an integer , the month whose cost you have to find, with .
A line starting with -1 marks the end of the input, and that line is not processed.
The input never makes the cost of any month from month 1 through month equal $0.00.
Output
For each line print the cost of month on its own line, in the form "Month N cost: $c". Put the month number from the input where N is, and the cost in dollars where c is.
Print exactly two digits after the decimal point, and group the whole-dollar part with a comma every three digits counting from the right. 1234567.89 dollars prints as 1,234,567.89, and 1000 dollars prints as 1,000.00.
Every cost you are asked for fits in the range of a double precision floating point number.