题目链接: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;
}