题目描述:
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.
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.
弗洛伊德算法,最短路径,AC代码:
using System;
namespace a1
{
class Program
{
public const int M = 50;
public static string[] countries = new string[M];
public static double[][] relation = new double[M][];
public static int find(string s, int n)
{
int mid = -1, high = n - 1, low = 0;
while (low <= high) {
mid = (low + high) / 2;
if (countries[mid] == s)
return mid;
else if (string.Compare(countries[mid], s) < 0)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void Main(string[] args)
{
int n, m, i, p, q, ce = 0;
string a, b;
double t;
string str = string.Empty;
while ((str = Console.ReadLine()) != null) {
n = Convert.ToInt32(str);
if (n == 0)
break;
ce++;
for (i = 0; i < n; i++) {
relation[i] = new double[n];
for (int j = 0; j < n; j++)
relation[i][j] = 0;
}
for (i = 0; i < n; i++) {
str = Console.ReadLine();
countries[i] = str;
relation[i][i] = 1;
}
Array.Sort(countries,0,n);
str = Console.ReadLine();
m = Convert.ToInt32(str);
for (i = 1; i <= m; i++) {
str = Console.ReadLine();
string[] s = str.Split(' ');
a = s[0];
t = Convert.ToDouble(s[1]);
b = s[2];
p = find(a, n);
q = find(b, n);
relation[p][q] = t;
}
Console.ReadLine();
for (i = 0; i < n; i++)
for (p = 0; p < n; p++)
for (q = 0; q < n; q++)
if (relation[p][i] * relation[i][q] > relation[p][q])
relation[p][q] = relation[p][i] * relation[i][q];
for (i = 0; i < n; i++)
if (relation[i][i] > 1)
break;
if (i >= n)
Console.WriteLine("Case {0}: No", ce);
else
Console.WriteLine("Case {0}: Yes", ce);
}
}
}
}