Exchange rates
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 157 accepted submission (s): 89
Problem descriptionnow that the loonie is hovering about par with the greenback, you have decided to use your $1000 entrance scholarship to engage in currency speculation. so you gaze into a crystal ball which predicts the closing exchange rate between Canadian and U. s. dollars for each of the next several days. on any given day, you can switch all of your money from Canadian to u. s. dollars, or vice versa, at the prevailing exchange rate, less a 3% commission, less any fraction of a cent.
Assuming your crystal ball is correct, what's the maximum amount of money you can have, in Canadian dollars, when you're done?
Inputthe input contains a number of test cases, followed by a line containing 0. each test case begins with 0 <D & #8804; 365, the number of days that your crystal ball can predict. D lines follow, giving the price of a U. s. dollar in Canadian dollars, as a real number.
Outputfor each test case, output a line giving the maximum amount of money, in Canadian dollars and cents, that it is possible to have at the end of the last prediction, assuming you may exchange money on any subset of the predicted days, in order.
Sample input3
1.0500
0.9300
0.9900
2
1.0500
1.1000
0
Sample output1001.60
1000.00 this is a simple DP question. You only need to keep the best status of each day. The dynamic equation is DP [I] [x] = max (DP [I-1] [X], DP [I-1] [Y] * Exchange rate) Where I represents days, X, Y is 0, 1 representing the US dollar and the Canadian dollar respectively. The Code is as follows:
1 # include <cstdio>
2 # include <cstring>
3 # include <cstring>
4 # include <cmath>
5 using namespace STD;
6
7 double DP [500] [2];
8
9 inline double max (Double X, Double Y)
10 {
11 if (x-y> 1e-6)
12 Return X;
13 return y;
14}
15
16 inline double to (double M, double XX)
17 {
18 return floor (M * XX * 0.97*100)/100;
19}
20
21 int main ()
22 {
23 int N;
24 while (scanf ("% d", & N), n)
25 {
26 double XX;
27 memset (DP, 0, sizeof (DP ));
28 DP [0] [1] = 1000.0; // The USD value of No. 0 storage, and the Canadian dollar value of No. 1 Storage
29 for (INT I = 1; I <= N; ++ I)
30 {
31 scanf ("% lf", & XX );
32 DP [I] [0] = max (DP [I-1] [0], to (DP [I-1] [1], 1.0/xx ));
33 DP [I] [1] = max (DP [I-1] [1], to (DP [I-1] [0], XX ));
34}
35 printf ("%. 2lf \ n", DP [N] [1]);
36}
37 return 0;
38}