Hi everyone! I am doing this problem and I have wrote the python solution, and now I am attempting the c++ version. This is my code:
// The following code is written by Bessie-Troublemaker
// Copyright Bessie-Troublemaker 1/16/2020
// This document may be reproduced for educational purposes only
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
bool is_add = true;
int count = 0;
int i = 0;
while (true) {
if (is_add) {
if (x <= y <= x + pow(2, i)) {
count += y - x;
cout << count << endl;
break;
} else {
if (x - pow(2, i) <= y <= x) {
count += x - y;
cout << count << endl;
break;
}
}
count += pow(2, (i+1));
is_add = !is_add;
i += 1;
}
}
return 0;
}
And the idea is similar my python code. Can anyone help?