#include <cstdlib>
#include <string.h>
#include <stdio.h>
int encode(int n, int x, int y) {
char N[100] = { 0 }, X[100] = { 0 }, Y[100] = { 0 };
itoa(n, N, 2);
//printf("n %d : %10s\n", n, N);
itoa(x, X, 2);
//printf("x %d : %10s\n", x, X);
itoa(y, Y, 2);
//printf("y %d : %10s\n", y, Y);
//printf("\n");
int i = strlen(N) - 1, j = strlen(X) - 1, k = strlen(Y) - 1;
int ret = 1;
while (true){
if (ret % 2 == 0 && N[i] == X[j] && X[j] != Y[k]){
return ret;
}
if (ret % 2 == 1 && N[i] == Y[k] && X[j] != Y[k]){
return ret;
}
i--;
j--;
k--;
if (i < 0){
N[0] = '0';
i = 0;
}
if (j < 0){
X[0] = '0';
j = 0;
}
if (k < 0){
Y[0] = '0';
k = 0;
}
ret++;
}
}
#include <cstdlib>
#include <stdio.h>
#include <string.h>
int decode (int n, int q, int h) {
char N[100] = { 0 }, Q[100] = { 0 } ;
itoa(n, N, 2);
itoa(q, Q, 2);
int i = strlen(N) - h, j = strlen(Q) - h;
if (h % 2 == 0){
//ret % 2 == 0 && N[i] == X[j] && X[j] != Y[k]
if (i < 0){
N[0] = '0';
i = 0;
}
if (j < 0){
Q[0] = '0';
j = 0;
}
return (N[i] == Q[j]);
}
else{
//ret % 2 == 1 && N[i] == Y[j] && X[j] != Y[k]
if (i < 0){
N[0] = '0';
i = 0;
}
if (j < 0){
Q[0] = '0';
j = 0;
}
return !(N[i] == Q[j]);
}
}
Compilation message
encoder.cpp: In function 'int encode(int, int, int)':
encoder.cpp:6:14: error: 'itoa' was not declared in this scope
itoa(n, N, 2);
^
decoder.cpp: In function 'int decode(int, int, int)':
decoder.cpp:7:14: error: 'itoa' was not declared in this scope
itoa(n, N, 2);
^