百练/ 北京大学2016研究生推免上机考试(校内)E: Exchange Rates (动态规划)

题目来源:http://noi.openjudge.cn/ch0206/2421/

2421: Exchange Rates

总时间限制: 1000ms  内存限制: 65536kB

描述

Nowthat the Loonie is hovering about par with the Greenback, you have decided touse your $1000 entrance scholarship to engage in currency speculation. So yougaze into a crystal ball which predicts the closing exchange rate betweenCanadian 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 acent. 
Assuming your crystal ball is correct, what's the maximum amount of money youcan have, in Canadian dollars, when you're done? 

输入

Theinput contains a number of test cases, followed by a line containing 0. Eachtest case begins with 0 <d ≤ 365, the number of days that yourcrystal ball can predict. d lines follow, giving the price of a U.S. dollar inCanadian dollars, as a real number.

输出

Foreach test case, output a line giving the maximum amount of money, in Canadiandollars and cents, that it is possible to have at the end of the lastprediction, assuming you may exchange money on any subset of the predicteddays, in order.

样例输入

3
1.0500
0.9300
0.9900
2
1.0500
1.1000
0

样例输出

1001.60
1000.00

-----------------------------------------------------

扫描二维码关注公众号,回复: 1060454 查看本文章

解题思路

一维动态规划

-----------------------------------------------------

代码

 

//E:Exchange Rates
//总时间限制: 1000ms 内存限制: 65536kB
//描述
//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 ≤ 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<fstream>
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("tm201601E.txt");
	int n,i,Int,Dec;
	double tmp = 1;
	int usd = 0, pusd = 0;
	int cand = 100000;
	while(fin >> n)
	{
		if (n==0)
		{
			break;
		}
		usd = 0;
		cand = 100000;
		for (i=0; i<n; i++)
		{
			fin >> tmp;
			pusd = usd;
			usd = max((int)(cand/tmp*.97), pusd);
			cand = max(cand, (int)(pusd*tmp*.97));
		}
		Int = cand/100;
		Dec = cand%100;
		if (Dec < 10)
		{
			cout << Int << ".0"  << Dec << endl;
		}
		else
		{
			cout << Int << "." << Dec << endl;
		}
	}
	fin.close();
#endif
#ifdef ONLINE_JUDGE
	int n,i,Int,Dec;
	double tmp = 1;
	int usd = 0, pusd = 0;
	int cand = 100000;
	while(cin >> n)
	{
		if (n==0)
		{
			break;
		}
		usd = 0;
		cand = 100000;
		for (i=0; i<n; i++)
		{
			cin >> tmp;
			pusd = usd;
			usd = max((int)(cand/tmp*.97), pusd);
			cand = max(cand, (int)(pusd*tmp*.97));
		}
		Int = cand/100;
		Dec = cand%100;
		if (Dec < 10)
		{
			cout << Int << ".0"  << Dec << endl;
		}
		else
		{
			cout << Int << "." << Dec << endl;
		}
	}
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80310463
今日推荐