杭电1217

21 篇文章 0 订阅

题目描述:

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.

弗洛伊德算法,最短路径,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);  
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值