#C267. Final Price Calculation
Final Price Calculation
Final Price Calculation
You are given an initial price \( P_0 \) and a series of discounts to apply sequentially. Each discount is either a percentage discount or a fixed discount. For a percentage discount, the discount is given as a percentage \( p \) (with \( 0 \leq p \leq 100 \)) and reduces the current price by \( p\% \). For a fixed discount, a non-negative amount is subtracted directly from the current price.
The computation is as follows:
[ P_{i} = \begin{cases} P_{i-1} - P_{i-1} \times \frac{p}{100} & \text{if discount is percentage}\ P_{i-1} - d & \text{if discount is fixed} \end{cases} ]
If at any point the price becomes negative, treat the final price as \(0.00\). The result should be rounded to two decimal places.
Input constraints:
- The initial price \( P_0 \) is a positive real number.
- For a percentage discount, \( p \) must be within \([0, 100]\).
- For a fixed discount, the discount amount must be non-negative.
If any of these conditions are violated, an error message is expected:
- If \( P_0 \) is not greater than 0, output:
Initial price must be > 0
- If a fixed discount is negative, output:
Fixed discount must be non-negative
- If a percentage discount is not in the range \( [0, 100] \), output:
Percentage discount must be between 0 and 100
inputFormat
The input is read from stdin and has the following format:
<initial_price> <n> <discount_type_1> <value_1> <discount_type_2> <value_2> ... <discount_type_n> <value_n>
Here:
<initial_price>
is a floating point number > 0.<n>
is an integer indicating the number of discounts.- Each subsequent line represents a discount. The discount type is either
P
for a percentage discount orF
for a fixed discount, followed by its value as a floating point number.
outputFormat
Output the final price to stdout as a floating point number rounded to two decimal places. In case of invalid input as described above, output the corresponding error message.
## sample100.0
2
P 10
F 5
85.00
</p>