Arbitrage
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar
3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar
0
题目大意:有n重货币,每种货币之间都是有汇率的,让你计算出是否在这些汇率当中存在经过交换之后本金增加的方式
思路:根据题意,本金的变化就是看汇率相乘是否结果会大于1,因此我们用Floyd的时候,就将相加的操作,变成相乘的操作
这样就能得到每两点之间和相乘汇率的结果,因为要看是否本金有所增加,因此只需要看当前点的值是否是大于1就行了,但要走注意初始化
#include <iostream>
#include<queue>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<cstdlib>
#include<cmath>
#include<map>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int INF=0x3f3f3f3f;
double dis[200];
int n;
int a[600];
int vis[600];
double e[200][200];
void Floyd()
{
rep(k,1,n)
rep(i,1,n)
rep(j,1,n)
if(e[i][j]<e[i][k]*e[k][j])
e[i][j]=e[i][k]*e[k][j];
}
int main()
{
int m;
string str;
ios::sync_with_stdio(0);cin.tie(0);
int num=1;
while(cin>>n,n)
{
map<string ,int>mm;
rep(i,1,n)
rep(j,1,n)
if(i==j) e[i][j]=1;
else e[i][j]=-1;
memset(e,0,sizeof e);
mm.clear();
int tot=1;
rep(i,1,n)
cin>>str,mm[str]=tot++;
cin>>m;
string str1,str2;double val;
rep(i,1,m)
{
cin>>str1>>val>>str2;e[mm[str1]][mm[str2]]=val;
// e[mm[str1]][mm[str1]]=1;e[mm[str2]][mm[str2]]=1;//千万不要这样初始化
}
cout<<"Case "<<num++<<": ";
Floyd();
bool flag=0;
rep(i,1,n)
if(e[i][i]>1)
{
flag=1;break;
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
博客介绍了货币套利概念,即利用汇率差异使一种货币增值。要求编写程序,根据输入的货币汇率列表判断是否存在套利可能。输入包含货币数量、名称、汇率表等,输出判断结果。思路是用Floyd算法,将相加操作变为相乘,看是否有大于1的结果。

被折叠的 条评论
为什么被折叠?



