-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (62 loc) · 2.04 KB
/
main.cpp
File metadata and controls
84 lines (62 loc) · 2.04 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <random>
using namespace std;
unsigned short query(const bool binary[], const unsigned short binaryLength) {
cout << "Q";
for (unsigned short index = 0; index < binaryLength; index++) {
cout << " " << binary[index];
cout.flush();
}
unsigned short result;
cin >> result;
return result;
}
void answer(bool binary[], unsigned short binaryLength) {
cout << "A";
for (unsigned short index = 0; index < binaryLength; index++) {
cout << " " << binary[index];
cout.flush();
}
}
unsigned short randomBit(const bool correctBinary[], const unsigned short binaryLength) {
bool isCorrect = true;
unsigned short randomPos = 0;
while (isCorrect) {
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, binaryLength);
randomPos = static_cast<unsigned short>(rand() % 100);
if (randomPos >= 0 && randomPos < binaryLength) {
if (!correctBinary[randomPos]) {
isCorrect = false;
}
}
}
return randomPos;
}
int main() {
unsigned short binaryLength; cin >> binaryLength;
bool binary[binaryLength];
bool correctBinary[binaryLength];
for (unsigned short index = 0; index < binaryLength; index++) {
binary[index] = false;
correctBinary[index] = false;
}
unsigned short prevCorrectCount;
unsigned short correctCount = query(binary, binaryLength);
unsigned short bitPoint;
while (correctCount != binaryLength) {
bitPoint = randomBit(correctBinary, binaryLength);
binary[bitPoint] = !binary[bitPoint];
prevCorrectCount = correctCount;
correctCount = query(binary, binaryLength);
if (prevCorrectCount < correctCount) {
correctBinary[bitPoint] = true;
} else {
binary[bitPoint] = !binary[bitPoint];
}
correctBinary[bitPoint] = true;
}
answer(binary, binaryLength);
return 0;
}