POJ 2403 Hay Points 解题报告

本文介绍了一个使用C++编程语言中的map容器来处理薪资计算的问题。通过建立两个map,一个用于存储词汇及其对应的金额,另一个用于读取描述并计算总薪资。程序能够有效地读取输入并进行相应的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*这道题主要练习了map的使用。 *使用map来操作,使得该题很简单。 */ #include <iostream> #include <map> using namespace std; int main() { map<string, int> dic; //用来保存数据字典 map<string, int> desc; //用来保存描述 typedef map<string, int>::value_type valType; int m, n; cin >> m >> n; for(int i = 0; i < m; i++) { string word; int dollar; cin >> word >> dollar; dic.insert(valType(word, dollar)); //不断插入新的字典单词 } /* map<string, int>::iterator map_it = dic.begin(); while(map_it != dic.end()) { cout << map_it->first << " " << map_it->second << endl; map_it++; } */ int sumSalary = 0; string desc_word; int descCount = 0; while(cin >> desc_word) { /*遇到'.'时的处理*/ if(desc_word == ".") { cout << sumSalary << endl; //输出当前总的salary sumSalary = 0; //置0,以处理下个描述的salary descCount++; if(descCount == n) break; else continue; } map<string, int>::iterator it = dic.find(desc_word); if(it != dic.end()) //若it不等于end,则表示在字典中找到该单词 sumSalary += it->second; } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值