#include #include #include #include using namespace std; const int MAX = 50; const int MAXAMT = 100000; string names[MAX]; int nNames; double rates[MAX][MAX]; int index(string name) { for(int j=0; j 0.0001) { cout << "ERROR: inconsistancy in data with " << names[i] << " and " << names[j] << endl; exit(-1); } return false; } void updateTable() { bool changesMade = true; while (changesMade) { changesMade = false; for(int i=0; i> n; while (n != 0) { icase++; for(i=0; i> r1 >> n1 >> n2 >> r2 >> n2; i1 = index(n1); i2 = index(n2); store(i1, i2, r2/r1); store(i2, i1, r1/r2); updateTable(); } cin >> r1 >> n1; i1 = index(n1); i=0; while(rates[i1][i] == 0.0 || r1*rates[i1][i] > MAXAMT) i++; int bestNum = (int)(r1*rates[i1][i]); if (fabs(bestNum - r1*rates[i1][i]) > 0.0000001) bestNum++; double bestDiff = bestNum*rates[i][i1] - r1; int bestIndex = i; double prevBest = -1; int prevIndex; i++; for(;i 0.0000001) num++; double diff = num*rates[i][i1] - r1; if (diff <= bestDiff) { prevBest = bestDiff; prevIndex = bestIndex; bestDiff = diff; bestNum = num; bestIndex = i; } } } if (fabs(prevBest - bestDiff) < 0.000001) { cout << "Error case " << icase << ": diffs too close for " << names[bestIndex] << " and " << names[prevIndex] << endl; exit(-1); } cout << "Case " << icase << ": " << bestNum << ' ' << names[bestIndex] << endl; cin >> n; } return 0; }