Currency Exchange
Time limit1sMemory limit128 MB
Given a 5x5 exchange-rate table where every conversion rounds to the nearest hundredth, simulate each trip and report the final dollar amount rounded to cents.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Math, Array
- Solved
- No attempts yet
Problem
When Isaac travels abroad — say, to France — he exchanges his US dollars for the local currency (French francs). An exchange rate is a real number: multiplying the number of dollars by the rate gives the number of francs. For example, if the exchange rate from US dollars to French francs is , then dollars becomes francs.
You can only hold hundredths of a unit of currency, so every exchanged amount is rounded to the nearest hundredth, with always rounded up to . Every exchange between any two currencies is rounded this way.
Some of Isaac's trips take him through several countries, exchanging the money of one foreign country directly for that of the next. When he finally comes home he exchanges whatever is left back into US dollars. Isaac wants to know how much of his unspent money is lost (or gained) to these exchange rates. Given the exchange rates and a sequence of trips, compute how many US dollars Isaac ends up with after each trip. Every trip starts and ends in US dollars.
Input
There are countries, numbered through ; country is the United States.
The first lines describe the exchange-rate matrix. Line contains real numbers; the -th number is the exchange rate from the currency of country to the currency of country . The rate from a country to itself is always .
Each of the following lines describes one trip in the form:
n c1 c2 … cn m
where and each is an integer from to giving, in order, the countries Isaac visits. The trip is therefore . The real number is the amount of US dollars Isaac starts the trip with.
A line whose first number is marks the end of the input; it contains no further numbers.
Output
For each trip, print one line with the number of US dollars Isaac has when he returns home, rounded to the nearest cent. Print the value in the usual way, with the cents shown after the decimal point and exactly two digits after it. If the amount is less than one dollar, print a in the dollars place (for example, 0.99).