用c++代码实现。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 fran
时间: 2024-03-14 07:46:56 浏览: 133
an empirical investigation of the arbitrage pricing theory.pdf
以下是C++代码实现:
```c++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;
int n, m;
double dist[N][N];
int main() {
while (cin >> n, n) {
memset(dist, 0, sizeof dist); // 初始化
for (int i = 1; i <= n; i++) {
dist[i][i] = 1; // 自己到自己的汇率为1
}
cin >> m;
for (int i = 0; i < m; i++) {
int a, b;
double c;
cin >> a >> b >> c;
dist[a][b] = c; // 记录汇率
}
// Floyd算法求最短路
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = max(dist[i][j], dist[i][k] * dist[k][j]);
}
}
}
bool flag = false;
for (int i = 1; i <= n; i++) {
if (dist[i][i] > 1) { // 存在套汇机会
flag = true;
break;
}
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
```
思路:使用Floyd算法求解任意两个货币之间的汇率,然后检查是否存在套汇机会。若存在,则输出"Yes",否则输出"No"。
阅读全文