-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamming.cpp
56 lines (46 loc) · 864 Bytes
/
hamming.cpp
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
/*
ID: th3c0r11
TASK: hamming
LANG: C++14
*/
#include <vector>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int n, b, d;
int distance(unsigned int a, unsigned int b)
{
auto c = a ^ b;
int ret = 0;
while (c) {
if (c & 0x1)
++ret;
c >>= 1;
}
return ret;
}
int main()
{
ifstream in{"hamming.in"};
ofstream out{"hamming.out"};
in >> n >> b >> d;
vector<unsigned int> values{0};
for (unsigned int i = 1; i < pow(2, b) && values.size() < n; ++i) {
bool distanceok = true;
for (int j = 0; j < values.size(); ++j) {
if (distance(i, values[j]) < d) {
distanceok = false;
break;
}
}
if (distanceok)
values.push_back(i);
}
for (int i = 0; i < values.size(); ++i) {
if (i % 10 == 9 && i != 0 || i == values.size() - 1)
out << values[i] << '\n';
else
out << values[i] << ' ';
}
}