UVa 11308

  题目大意:给出一些原料和价钱和若干份菜谱,每份菜谱都标明所需的原料和数量,找出所有不超过预算的菜谱。

  没什么好说的,主要是对map的运用。

 1 #include <cstdio>
 2 #include <string>
 3 #include <map>
 4 #include <iostream>
 5 #include <cctype>
 6 #include <algorithm>
 7 using namespace std;
 8 typedef map<string, int> msi;
 9 
10 struct Recipe
11 {
12     string name;
13     int cost;
14     bool operator < (const Recipe& r) const
15     {
16         if (cost != r.cost)  return cost < r.cost;
17         return name < r.name;
18     }
19 }recipe[110];
20 
21 int main()
22 {
23 #ifdef LOCAL
24     freopen("in", "r", stdin);
25 #endif
26     int T;
27     cin >> T;
28     getchar();
29     msi ingredient;
30     while (T--)
31     {
32         string title;
33         getline(cin, title);
34         transform(title.begin(), title.end(), title.begin(), ::toupper);
35         int m, n, b;
36         cin >> m >> n >> b;
37         getchar();
38         ingredient.clear();
39         string str;
40         int x; 
41         for (int i = 0; i < m; i++)
42         {
43             cin >> str >> x;
44             getchar();
45             ingredient[str] = x;
46         }
47         for (int i = 0; i < n; i++)
48         {
49             getline(cin, recipe[i].name);
50             recipe[i].cost = 0;
51             int k;
52             cin >> k;
53             getchar();
54             for (int j = 0; j < k; j++)
55             {
56                 cin >> str >> x;
57                 getchar();
58                 recipe[i].cost += ingredient[str]*x;
59             }
60         }
61         sort(recipe, recipe+n);
62         cout << title << endl;
63         if (recipe[0].cost > b)
64         {
65             cout << "Too expensive!" << endl << endl;
66             continue;
67         }
68         for (int i = 0; i < n; i++)
69         {
70             if (recipe[i].cost > b)  break;
71             cout << recipe[i].name << endl;
72         }
73         cout << endl;
74     }
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3293545.html