-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.hpp
More file actions
62 lines (43 loc) · 1.18 KB
/
Solution.hpp
File metadata and controls
62 lines (43 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Created by Evan on 2016/3/16.
//
#ifndef PROBLEM299_SOLUTION_HPP
#define PROBLEM299_SOLUTION_HPP
#include <string>
#include <map>
using std::string;
using std::map;
class Solution {
public:
string getHint(string secret, string guess) {
int index, size = secret.size();
int bulls = 0, cows = 0;
int statistic[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(index = 0; index < size; ++ index)
{
if(secret[index] == guess[index])
{
++bulls;
secret[index] = guess[index] = 'x';
}
}
for(index = 0; index < size; ++index)
{
if(secret[index] == 'x') continue;
++statistic[secret[index] - '0'];
}
for(index = 0; index < size; ++index)
{
if(guess[index] == 'x') continue;
if(statistic[guess[index] - '0'] > 0)
{
++cows;
--statistic[guess[index] - '0'];
}
}
char resultString[128];
snprintf(resultString, 128, "%dA%dB", bulls, cows);
return string(resultString);
}
};
#endif //PROBLEM299_SOLUTION_HPP_H