水题题解IIIII

该文是一个关于利用编程解决套汇盈利问题的题目描述,涉及货币汇率转换和最短路径算法。给定不同货币间汇率,判断是否存在通过多次兑换获取更多货币的可能。程序通过构建货币汇率矩阵并使用弗洛伊德算法检查循环是否能带来利润。
摘要由CSDN通过智能技术生成

一个人想套汇赚钱,所谓套汇,是指用货币之间的汇率差赚钱,比如 22 美元能换 11 英镑,11 英镑能换 1010 法郎,100100 法郎能换 2121 美元,则他用 2020 美元经过上述一系列转换能换到 2121 美元。

输入格式

本题目包含多组测试,注意输入方式。

第一行为一个正整数n(1 \le n\le 30)n(1≤n≤30),表示货币种类。

之后 nn 行为每种货币的名称,保证名称中不含有空格。

接着一行有一个正整数 mm,表示随后汇率表的长度。

最后 mm 行包括一个字符串s_1s1​,一个实数 a_iai​,以及另一个字符串 s_2s2​,意为货币s_1s1​ 与货币 s_2s2​ 的汇率为 a_iai​,s_1 \times a_i = s_2s1​×ai​=s2​,注意这是单方向兑换,表示可以用 11 个单位的 s_1s1​ 可以兑换到 a_iai​ 个单位的 s_2s2​,货币表中未出现关系的货币两两不能互相兑换。

当 n=0n=0 输入结束。

输出格式

对于每一个测试点如果存在能赚钱的情况则输出 YESYES 否则输出 NONO。

每行输出末尾不能有多余空格。

输入样例

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

输出样例

Case 1: Yes
Case 2: No
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e6 + 5, INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
	if (b) while ((a %= b) && (b %= a));
	return a + b;
}
int n, m, idx;
int head[N], brr[N];
int dis[N];
bool book[N];
double arr[50][50];
struct node
{
	int a, b, w;
	int ne;
}sta[N];

void add(int a, int b, int w)
{
	//sta[idx].a = a;
	sta[idx].b = b;
	sta[idx].w = w;
	sta[idx].ne = head[a];
	head[a] = idx++;
}

void floyd()
{
	for (int k = 1;k <= n;k++)
	{
		for (int i = 1;i <= n;i++)
		{
			for (int j = 1;j <= n;j++)
			{
				if (arr[i][j] < arr[i][k] * arr[k][j])
				{
					arr[i][j] = arr[i][k] * arr[k][j];
				}
			}
		}
	}
}

int main()
{
        int k=0;
	while (scanf("%d", &n) && n!=0)
	{
               k ++;
		map<string, int>mp;
		

			for (int i = 1;i <= n;i++)
			{
				string s1;
				cin >> s1;
				mp[s1] = i;
			}

			for (int i = 1;i <= n;i++)
			{
				for (int j = 1;j <= n;j++)
				{
					if (i == j)
					{
						arr[i][j] = 1;
					}
					else
					{
						arr[i][j] = 0;
					}
				}
			}

			scanf("%d", &m);
			while (m--)
			{
				string s1, s2;
				double cnt;
				cin >> s1 >> cnt >> s2;
				arr[mp[s1]][mp[s2]] = cnt;
			}

			floyd();

			int flag = 0;
			for (int i = 1;i <= n;i++)
			{
				if (arr[i][i] > 1)
				{
					flag = 1;
					break;
				}
			}

			if (flag == 1)
			{
				printf("Case %d: Yes\n", k);
			}
			else
			{
				printf("Case %d: No\n", k);
			}
		
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值