Sometimes Issac's trips take him to many countries and he exchanges money from one foreign country for that of another. When he finally arrives back home, he exchanges his money back for US dollars. This has got Issac thinking about how much if his unspent US dollars is lost (or gained!) to these exchange rartes. You'll compute how much money Issac ends up with if he exchanges it many times. You'll always start with US dollars and you'll always end with US dollars.
Input
The first 5 lines of input will be the exchange rates between 5 countries, numbered 1 through 5. Line i will five the exchange rate from country i to each of the 5 countries. Thus the jth entry of line i will give the exchange rate from the currency of country i to the currency of country j. the exchange rate form country i to itself will always be 1 and country 1 will be the US. Each of the next lines will indicate a trip and be of the form
N c1 c2 ... cn m
Where 1 ≤ n ≤ 10 and c1, ..., cn are integers from 2 through 5 indicating the order in which Issac visits the countries. (A value of n = 0 indicates end of input, in which case there will be no more numbers on the line.) So, his trip will be 1 -> c1 -> c2 -> ... -> cn -> 1. the real number m will be the amount of US dollars at the start of the trip.
Output
Each trip will generate one line of output giving the amount of US dollars upon his return home from the trip. The amount should be fiven to the nearest cent, and should be displayed in the usual form with cents given to the right of the decimal point, as shown in the sample output. If the amount is less than one dollar, the output should have a zero in the dollars place.
Sample Input
1 1.57556 1.10521 0.691426 7.25005
0.634602 1 0.701196 0.43856 4.59847
0.904750 1.42647 1 0.625627 6.55957
1.44616 2.28059 1.59840 1 10.4843
0.137931 0.217555 0.152449 0.0953772 1
3 2 4 5 20.00
1 3 100.00
6 2 3 4 2 4 3 120.03
0
Sample Output
19.98
99.99
120.01
Source: East Central North America 2001 Practice
my code:
#include <iostream>
#include <iomanip>
using namespace std;
double cut(double ans){
double tem;
tem=ans*100;
int b=tem;
tem-=b;
return ans-tem/100;
}
double reduce(double chg){
int a=chg;
double ans=chg-a,tem=0;
int b=ans*100;
ans-=b/100.0;
if(ans>0.005){
ans+=0.01;
}
ans+=b/100.0;
return cut(ans)+a;
}
int main(){
double a[5][5];
for(int i=0;i<5;++i) for(int j=0;j<5;++j) cin>>a[i][j];
int n;
while(1){
cin>>n;
if(n==0)break;
int *t=new int [n+2];
t[0]=1;
for(int i=1;i<=n;++i){cin>>t[i];}
t[n+1]=1;
double chg,tem;
cin>>chg;
for(int i=0;i<n+1;++i){
chg*=a[t[i]-1][t[i+1]-1];
chg=reduce(chg);
}
cout<<setprecision(2)<<fixed<<chg<<endl;
delete []t;
}
return 0;
}