Codeforces Round #485 (Div. 2)-A. Infinity Gauntlet——算法笔记

题目链接:http://codeforces.com/problemset/problem/987/A
题目描述:

You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems:
the Power Gem of purple color,
the Time Gem of green color,
the Space Gem of blue color,
the Soul Gem of orange color,
the Reality Gem of red color,
the Mind Gem of yellow color.
Using colors of Gems you saw in the Gauntlet determine the names of absent Gems.

输入:

In the first line of input there is one integer n (0≤n≤6) — the number of Gems in Infinity Gauntlet.
In next n lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowercase English letters.

输出:

In the first line output one integer m (0≤m≤6) — the number of absent Gems.
Then in m lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase.

样例:

在这里插入图片描述

说明:

In the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space.
In the second sample Thanos doesn’t have any Gems, so he needs all six.

题解:

对于这种有一对一关系的数据,我想到的就是用 map 来存储数据(不知道还有没有别的好方法)。map 还可以将不同类型的数据变得有关系。
这题可以用 map<string, string> 存储,将宝石的名称和颜色关联起来。之后,由于输出要求是把没有输入的颜色的宝石的名称输出。所以,就把输入颜色的宝石名称处理一下(因为它们不用输出),这里我把已输入的颜色的 value 值赋空。所以,到最后直接遍历输出 value 值不为空的即可。

参考代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

#define INF 0x3f3f3f
#define ll long long
#define clean(arrays) memset(arrays, 0, sizeof(arrays))

// map 这样初始化要编译时版本要高于 C++ 11
map<string, string> a = {
    {"purple", "Power"}, {"green", "Time"}, {"blue", "Space"},
    {"orange", "Soul"}, {"red", "Reality"}, {"yellow", "Mind"}
    };

int main()
{
    int n;
    string str;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> str;
        a[str] = "";	// 这里是双引号
    }
    cout << 6 - n << endl;
    map<string, string>::iterator it;	//使用了迭代器遍历
    for (it = a.begin(); it != a.end(); it++)
    {
        if (it->second != "") {		// value 值不为空就输出
            cout << it->second << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值