Skip to content

Instantly share code, notes, and snippets.

@likejazz
Last active July 22, 2017 10:35
Show Gist options
  • Save likejazz/dc5b530affdc7a90aff34eea9bfa89ab to your computer and use it in GitHub Desktop.
Save likejazz/dc5b530affdc7a90aff34eea9bfa89ab to your computer and use it in GitHub Desktop.
Minimum Moves
#include <iostream>
using namespace std;
int diff = 0;
// C++ get each digit in integer
// https://stackoverflow.com/a/4615187/3513266
void calc_each_moves(int a, int m) {
// the variable `a` is used as a reference.
if (a >= 10)
calc_each_moves(a / 10, m / 10);
int ai = a % 10;
int mi = m % 10;
diff += abs(ai - mi);
}
int main() {
int an, mn;
// inputs for Andrea's array.
cin >> an;
int a[an];
for (int i = 0; i < an; i++) {
cin >> a[i];
}
// inputs for Maria's array.
cin >> mn;
int m[mn];
for (int i = 0; i < mn; i++) {
cin >> m[i];
}
// calculate the each moves.
for (int i = 0; i < an; i++) {
calc_each_moves(a[i], m[i]);
}
// print the whole moves.
cout << diff << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment