题解 | Counterfeit Dollar
Counterfeit Dollar
https://www.nowcoder.com/practice/7c4430f6aa2a48f5a817e31d1c26773e
#include <iostream>
#include <cstring>
#include <algorithm>
#include<string>
#include<map>
using namespace std;
map<char, bool>heavy, light, equall, besides;
int main()
{
for (int i = 0; i < 3; i++) {
string str1, str2, op;
cin >> str1 >> str2 >> op;
if (op == "even") {
for (int i = 0; i < str1.size(); i++)
equall[str1[i]] = true, equall[str2[i]] = true;
}
if (op == "down") {
besides.clear();
for (int i = 0; i < str1.size(); i++) {
heavy[str2[i]] = true; light[str1[i]] = true;
besides[str1[i]] = true; besides[str2[i]] = true;
}
for (int i = 0; i < 12; i++) {
if (besides.count('A' + i))continue;
equall['A' + i] = true;
}
}
if (op == "up") {
besides.clear();
for (int i = 0; i < str1.size(); i++) {
heavy[str1[i]] = true; light[str2[i]] = true;
besides[str1[i]] = true; besides[str2[i]] = true;
}
for (int i = 0; i < 12; i++) {
if (besides.count('A' + i))continue;
equall['A' + i] = true;
}
}
}
//最后排查剩下的就是假币
for (int i = 0; i < 12; i++) {
if (equall.count('A' + i))continue;
//if (besides.count('A' + i))continue;
if (heavy.count('A' + i) && light.count('A' + i))continue;
//一会重一会轻也是z真币
if (heavy.count('A' + i))
printf("%c is the counterfeit coin and it is heavy.", 'A' + i);
else
printf("%c is the counterfeit coin and it is light.", 'A' + i);
}
}
查看20道真题和解析