Every year, an elephant qualifies to the Arab Collegiate Programming Competition. He graduated this year, but that’s irrelephant. What’s important is that the location of the competition might not have been the same every year. Therefore, after every trip, he always has leftover money in the currency of the country he visited.
Now he wants to see how much Jordanian Dinars he has after all those competitions. Can you help him convert the leftover money from all competitions to Jordanian Dinar, if that makes any cents?
The first line of input is T – the number of test cases.
The first line of each test case contains C and N (1 ≤ C, N ≤ 100000), the number of currency types and the number of competitions, respectively.
The next C lines each contain the name of the currency Ci of maximum length 10 in lowercase and/or uppercase letters, and the value Vi of that currency in Jordanian Dinar (0 < Vi ≤ 1000). The names are case-sensitive.
The next N lines each contains an amount left over from each competition (0 ≤ Ni ≤ 1000), and the name of the currency of that amount (it is guaranteed that the name was either given in the input or is “JD”).
For each test case, print on a single line the total amount of money he has in Jordanian Dinar(JD) rounded to 6 decimal digits.
1 3 5 dollar 0.71 euro 0.76 turkish 0.17 5.1 dollar 6 dollar 7 turkish 3 euro 1.1 JD
12.451000
题意:给出n种货币的名称和对应的价值,货币JD价值为1。有m次交易,问经过m次交易后获得价值与货币JD等值的货币价值。
分析:直接用STL中的map,使货币与其价值对应起来计算即可。
代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
const int MX = 1e5 + 5;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
map<string, double> mp;
for(int i = 0; i < n; i++){
string k;
double x;
char c[15];
scanf("%s", c);
scanf("%lf", &x);
for(int i = 0; i < strlen(c); i++){
k += c[i];
}
mp[k] = x;
}
mp["JD"] = 1.0;
double ans = 0;
for(int i = 0; i < m; i++){
string k;
double x;
char c[15];
scanf("%lf", &x);
scanf("%s", c);
for(int i = 0; i < strlen(c); i++){
k += c[i];
}
ans += (mp.find(k) -> second) * x;
}
printf("%.6lf\n", ans);
}
}