当前位置:编程学习 > C/C++ >>

POJ 2240 Arbitrage

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11539   Accepted: 4845
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, 易做图 a profit of 5 percent. www.zzzyk.com

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 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
Source

Ulm Local 1996
[Submit] [Go Back] [Status] [Discuss]

注意:开数组的时候一定要注意数据的类型,因为这个错了很多次,到最后才注意到res数组开成整型的了

[cpp] 
#include <iostream> 
#include <string> 
using namespace std; 
int statck[1000000]; 
int status[100]; 
double res[100]; 
class num 

    public: 
    int end; 
    double val; 
    int next; 
}a[1000000]; 
int b[100]; 
int sum[100],tag,w; 
string s1[100]; 
int n; 
int main() 

    void deal(int k); 
    int i,j,m,s,t,x,y,z; 
    string s2,s3; 
    double val; 
    t=1; 
    while(cin>>n) 
    { 
        if(!n) 
        { 
            break; 
        } 
        for(i=1;i<=n;i++) 
        { 
            cin>>s1[i]; 
        } 
        cin>>m; 
        memset(b,-1,sizeof(b)); 
        z=0; 
        for(i=1;i<=m;i++) 
        { 
            cin>>s2; 
            cin>>val; 
            cin>>s3; 
            for(j=1;j<=n;j++) 
            { 
                if(s1[j]==s2) 
                { 
                    x=j; 
                } 
                if(s1[j]==s3) 
                { 
                    y=j; 
                } 
            } 
            a[z].end=y; a[z].val=val; 
            a[z].next=b[x]; 
            b[x]=z;  z++; 
        } 
        for(i=1;i<=n;i++) 
        { 
            tag=0; 
            deal(1); 
            if(tag==1) 
            { 
                break; 
            } 
        } 
        cout<<"Case "<<t<<":"<<" "; 
        t++; 
        if(i==n+1) 
        { 
            cout<<"No"<<endl; 
        }else 
        { 
            cout<<"Yes"<<endl; 
        } 
    } 
    return 0; 

void deal(int k) 

    int i,j,base,top; 
    int x; 
    for(i=1;i<=n;i++) 
    { 
       res[i]=0; 
    } 
    res[k]=100.0; 
    base=top=0; 
    memset(status,0,sizeof(status)); 
    memset(su

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,