-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing-integer.cpp
More file actions
60 lines (51 loc) · 911 Bytes
/
missing-integer.cpp
File metadata and controls
60 lines (51 loc) · 911 Bytes
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
#include<iostream>
#include<math.h>
const int n=10;
const int m= floor(log2(n))+1;
int A[m][n] = {
{0,0,1,0,0,0,0,1,1,0},
{0,0,0,1,1,1,0,0,0,0},
{1,0,1,1,0,0,0,0,0,1},
{1,1,0,1,0,1,0,1,0,0}
};
int main() {
int i,j,t, n0, n1, ntemp=n;
int B[n], res[m];
for (i=0; i<m; i++)
res[i]=-1;
for (i=0; i<n; i++)
B[i]=i;
for (i=(m-1); i>=0; i--)
{
n0 = n1 = 0;
for (j=0; j<ntemp; j++)
{
if (A[i][B[j]] == 0)
n0++;
else
n1++;
}
if (n0 - n1 == 2 || n0 - n1 == 1)
res[i] = 1;
else if (n0 - n1 == 0 || n0 - n1 == -1)
res[i] = 0;
else
std::cout<<"Warning"<<std::endl;
for (j=0, t=0; j<ntemp; j++) {
if (res[i] == A[i][B[j]]) {
B[t] = B[j];
t++;
}
}
if (t)
ntemp = t;
else if (ntemp % 2 == 0)
ntemp = (ntemp / 2);
else
ntemp = floor(ntemp/2.0);
}
for (i=0; i<m; i++)
std::cout << res[i];
std::cout<<std::endl;
return 0;
}