题目描述
我们现在有四个槽,每个槽放一个球,颜色可能是红色(R)、黄色(Y)、绿色(G)或蓝色(B)。例如,可能的情况为RGGB(槽1为红色,槽2、3为绿色,槽4为蓝色),作为玩家,你需要试图猜出颜色的组合。比如,你可能猜YRGB。要是你猜对了某个槽的颜色,则算一次“猜中”。要是只是猜对了颜色但槽位猜错了,则算一次“伪猜中”。注意,“猜中”不能算入“伪猜中”。
给定两个string A和guess。分别表示颜色组合,和一个猜测。请返回一个int数组,第一个元素为猜中的次数,第二个元素为伪猜中的次数。
测试样例:
"RGBY","GGRR"
返回:[1,1]
class Result {
public:
vector<int> calcResult(string A, string guess) {
// write code here
vector<int> res(2);
string tempA = A;
string tempguess = guess;
for(int i = 0; i < 4; i++)
{
if(tempA.at(i) == tempguess.at(i) )
{
tempA.at(i) = '1';
tempguess.at(i) = '2';
res[0] += 1;
}
}
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
if(tempA.at(k) == '3' || tempguess.at(j) == '4')
{
continue;
}
if(tempA.at(k) == tempguess.at(j))
{
res[1] += 1;
tempA.at(k) = '3';
tempguess.at(j) = '4';
}
}
}
return res;
}
};
class Result {
public:
vector<int> calcResult(string A, string guess) {
// write code here
vector<int> res(2);
string tempA = A;
string tempguess = guess;
for(int i = 0; i < 4; i++)
{
if(tempA.at(i) == tempguess.at(i) )
{
tempA.at(i) = '1';
tempguess.at(i) = '2';
res[0] += 1;
}
}
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
if(tempA.at(k) == '3' || tempguess.at(j) == '4')
{
continue;
}
if(tempA.at(k) == tempguess.at(j))
{
res[1] += 1;
tempA.at(k) = '3';
tempguess.at(j) = '4';
}
}
}
return res;
}
};
这是我的程序,虽然测试通过了,一定要想清楚,然后再动笔。处理不当的地方请大家指出