UVa 11308 - Bankrupt Baker

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

  没什么好说的,主要是对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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值