TJU Price

35 篇文章 0 订阅
13 篇文章 0 订阅

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?

Input

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 si is 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.

Output

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.

Sample Input

3 2
rice 0.5
egg 2.0
meat 5.0
meat
rice

Sample Output

5.50


题意概述:题目意思没什么好说的,就是一个水题。如果用标准库中的 map ,则会非常方便;但是如果用关联数组的话,可能要费点事,因为对输入的每一个字符串,要循环 找到,可能效率就不如map了。

解题思路:思路简单,先输入每一组dish和对应的price  。输入完成后,输入dish,在map中找到对应的price,加上即可。值得注意的是,本题要求连续输入n和m,不要求结束。

源代码:
#include<iostream>
#include<map>
#include<iomanip> 
using namespace std;
int main()
{
    int n,m;
    double price,sum=0;
    string name;
    map<string,double>Set;
    while(cin>>n>>m)
    {
         for(int i=0;i<n;++i)
         {
             cin>>name>>price;
             Set[name]=price;
         }
         for(int i=0;i<m;++i)
         {
             cin>>name;
             sum+=Set[name];
         }
         cout<<setiosflags(ios::fixed)<<setprecision(2)<<sum<<endl;//按固定格式输出
         Set.clear();
         sum=0;
    }
    return 0;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值