-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm_1
71 lines (63 loc) · 1.53 KB
/
hm_1
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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <map>
#include <string>
using namespace std;
typedef map<int, int> M;
class ChooseNum
{
public:
ChooseNum(const string & digits) : digits_(digits) {}
char operator()() const { return digits_[rand () % digits_.size()]; }
private:
const string & digits_;
};
class CowsBulls
{
public:
CowsBulls(M & cm, M & gm, int & bull)
: cm_(cm), gm_(gm), bull_(bull = 0) {}
char operator()(char c, char g)
{
return ++cm_[c],
++gm_[g],
bull_ += (c == g),
g;
}
private:
M &cm_, &gm_;
int & bull_;
};
class CountCowBull
{
public:
CountCowBull(M & cm, M & gm, int & cow)
: cm_(cm), gm_(gm), cow_(cow = 0) {}
void operator()(char c) const
{
cow_ += min(cm_[c], gm_[c]);
}
private:
M &cm_, &gm_;
int & cow_;
};
int main()
{
const string digits("0123456789");
string comb(2, '.'), guess;
int cow, bull = 0;
M cm, gm;
srand(static_cast<unsigned int>(time(0))), generate(comb.begin(), comb.end(), ChooseNum(digits));
while (bull < comb.size())
cout << "\nguess -> ",
cin >> guess,
guess.resize(comb.size(), ' '),
cm = gm = M(),
transform(comb.begin(), comb.end(), guess.begin(), guess.begin(), CowsBulls(cm, gm, bull)),
for_each(digits.begin(), digits.end(), CountCowBull(cm, gm, cow)),
cout <<"cows "<< cow << " bulls " << bull;
cout << " - solved!\n";
system("pause");
}