题目链接:http://acm.tju.edu.cn/toj/showp3855.html
3855. Price
Ant is a student in Tianjin University. Everyday, he has to go to the restaurant in school. Recently, some other students complain about the high price of restaurant dishes. Ant is a poor man, he don't want to spend too much. Then he wants to know the total price of the dishes he wants to order before he pays for them. Now he wants to write a program to help him calculate the price. Could you help him?
There are several test case in the input file. For each test case, the first line contains two integer n and m, seperated by a single space. n is the number of all the dishes and m is the number of the dishes that Ant wants to order. Then n ( 0 < n ≤ 100) lines follows and each line contains one string si and an real number ai. The string siis the name of the i-th dish and the real number ai is the price of the i-th dish. The length of string si is less than 10. The next m following lines describe the dishes which Ant wants to order and each of them contains a string which is the name of dish. Every dish is calculated only once.
For each test case, output a real number which is the total price in a line. The total price should be printed with two digits to the right of the decimal point.
水题,上代码:
#include <stdio.h> #include <iostream> using namespace std; int main(){ string dish[101],order; int m,n; double price[101],sum; while(~scanf("%d%d",&n,&m)){ sum=0; for(int i=0;i<n;i++) { cin>>dish[i];scanf("%lf",&price[i]);} for(int i=0;i<m;i++){ cin>>order; for(int j=0;j<n;j++) if(dish[j]==order) sum+=price[j]; } printf("%.2lf\n",sum); } }