zoukankan      html  css  js  c++  java
  • HDU 1217 Arbitrage (Floyd)

    Arbitrage

    http://acm.hdu.edu.cn/showproblem.php?pid=1217

    Problem Description
    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 file 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
     
    Sample Output
    Case 1: Yes
    Case 2: No
     

     解题思路:将货币种类的名字转化为数字,用其作为货币相互转换的下标,然后用Floyd算出货币之间转换后的最多货币,再寻找自己转换为自己大于1的情况,如果有就输出Yes, 没有就输出No

    解题代码:

     1 // File Name: Arbitrage 1217.cpp
     2 // Author: sheng
     3 // Created Time: 2013年07月19日 星期五 15时57分20秒
     4 
     5 #include <math.h>
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <string>
     9 #include <iostream>
    10 #include <map>
    11 using namespace std;
    12 
    13 const int max_n = 32;
    14 map<string, int> k;
    15 
    16 double cas[max_n][max_n];
    17 
    18 int main()
    19 {
    20     string str1, str2;
    21     int n, m, T = 1;
    22     while (scanf("%d", &n) != EOF && n)
    23     {
    24         k.clear();
    25         memset (cas, 0, sizeof (cas));
    26         for (int i = 1; i <= n; i ++)
    27         {
    28             cin >> str1;
    29             k[str1] = i;//转换为数字
    30         }
    31         scanf ("%d", &m);
    32         while (m --)
    33         {
    34             double temp;
    35             cin >> str1;
    36             cin >> temp;
    37             cin >> str2;
    38             cas[k[str1]][k[str2]] = temp;
    39         }
    40         for (int i = 1; i <= n; i ++)
    41             for (int j = 1; j <= n; j ++)
    42                 for (int k = 1; k <= n; k ++)
    43                 {
    44                     if (cas[j][k] < cas[j][i] * cas[i][k])
    45                         cas[j][k] = cas[j][i] * cas[i][k];
    46                 }
    47         int i;
    48         for (i = 1; i <= n; i ++)
    49             if (cas[i][i] > 1)
    50             {
    51                 printf ("Case %d: Yes
    ", T++);
    52                 break;
    53             }
    54         if (i > n)
    55             printf ("Case %d: No
    ", T++);
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    siteserver学习笔记
    移动端开发适配的2中方案
    移动端中适配问题
    2倍图3倍图怎么用
    常用的网站收藏
    关于用h5实现移动端的知识梳理
    悬浮广告代码
    vue中添加echarts
    VUE中给template组件加背景
    纯CSS控制背景图片100%自适应填充布局
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3200916.html
Copyright © 2011-2022 走看看