首页 > ACM题库 > HDU-杭电 > HDU 1903 Exchange Rates-动态规划-[解题报告] C++
2013
12-23

HDU 1903 Exchange Rates-动态规划-[解题报告] C++

Exchange Rates

问题描述 :

Now 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?

输入:

The 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.

输出:

For 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.

样例输入:

3
1.0500
0.9300
0.9900
2
1.0500
1.1000
0

样例输出:

1001.60
1000.00

#include <cstdio>
 #include <cstring>
 #include <cstring>
 #include <cmath>
 using namespace std;
 
 double dp[500][2];
 
 inline double Max( double x, double y )
 {
     if( x - y > 1e-6 )
         return x;
     return y;
 }
 
 inline double to( double m, double xx )
 {
     return floor( m * xx * 0.97 * 100 ) / 100;
 }
 
 int main()
 {
     int N;
     while( scanf( "%d", &N ), N )
     {
         double xx;
         memset( dp, 0, sizeof( dp ) );
         dp[0][1] = 1000.0;  // 0号存储美元值,1号存储加元值
         for( int i = 1; i <= N; ++i )
         {
             scanf( "%lf", &xx );
             dp[i][0] = Max( dp[i-1][0], to( dp[i-1][1], 1.0 / xx ) );
             dp[i][1] = Max( dp[i-1][1], to( dp[i-1][0], xx ) );
         }
         printf( "%.2lf\n", dp[N][1] );
     }
     return 0;
 }

解题报告转自:http://www.cnblogs.com/Lyush/archive/2011/09/02/2163969.html


  1. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count