杭电Shopping

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648

今天开始正式刷杭电,也开始了学习hash和string 的学习,也正式开始了数据结构刷题的记录;

这道题是应用map的典型例题;

首先,我们前面已经介绍过了map,map是一种从键key到值value的映射;

对于用一个值对应另一个值的映射关系是最合适的应用,也是STL的具体应用;

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     std::ios::sync_with_stdio(false);
 6     cin.tie(0);
 7     cout.tie(0);
 8     int n,m,c;
 9     map<string ,int >shop;//map建立
10     while(cin>>n)
11     {
12         string s;
13         for(int i=1;i<=n;i++)
14         cin>>s;
15         cin>>m;
16         while(m--)
17         {
18             for(int i=1;i<=n;i++)
19             {
20                 cin>>c>>s;
21                 shop[s]+=c;//名为s的商店涨价c
22             }
23             int rank=1;
24             map<string ,int >::iterator it;//迭代器
25             for(it=shop.begin();it!=shop.end();it++)
26             if(it->second>shop["memory"])//键值大于memory商店
27             rank++;//位置+1
28             cout<<rank<<endl;
29         }
30         shop.clear();
31     }
32     return 0;
33 }

 

posted @ 2022-05-03 14:46  江上舟摇  阅读(17)  评论(0编辑  收藏  举报