Problem p00211 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stack>
#include <stdio.h>

using namespace std;

long long calc(long long x, long long y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

struct Student {
  long long int distance, speed;
};

int main() {

  int n;
  long long int c_speed, c_distance, work;
  Student students[10];

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      scanf("%lld %lld", &students[i].distance, &students[i].speed);
      //?´??????????
      if (students[i].distance > students[i].speed) {
        work = calc(students[i].distance, students[i].speed);
      } else {
        work = calc(students[i].speed, students[i].distance);
      }
      students[i].distance /= work;
      students[i].speed /= work;
    }

    //?????????????°???¬?????°????±???????<??????????????????:??????????????¢????????????????????????????????§??????>

    //???????????¢???????°???¬?????°????±???????
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        c_distance = students[i].distance;
      } else {
        if (c_distance >= students[i].distance) {
          work = calc(c_distance, students[i].distance);
          c_distance = ((c_distance / work) * students[i].distance);
        } else {
          work = calc(students[i].distance, c_distance);
          c_distance = ((c_distance / work) * students[i].distance);
        }
      }
    }

    //??????????????????????????????????????????
    for (int i = 0; i < n; i++) {
      students[i].speed *= (c_distance / students[i].distance);
    }

    //??????????????§??¬?´???°????±???????<?????????????????°??????>
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        work = students[i].speed;
      } else {
        if (work > students[i].speed) {
          work = calc(work, students[i].speed);
        } else {
          work = calc(students[i].speed, work);
        }
      }
    }

    for (int i = 0; i < n; i++) {
      printf("%lld\\n", students[i].speed / work);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <math.h>
#include <stack>
#include <stdio.h>

using namespace std;

long long calc(long long x, long long y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

struct Student {
  long long int distance, speed;
};

int main() {

  int n;
  long long int c_speed, c_distance, work;
  Student students[10];

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      scanf("%lld %lld", &students[i].distance, &students[i].speed);
      //?´??????????
      if (students[i].distance > students[i].speed) {
        work = calc(students[i].distance, students[i].speed);
      } else {
        work = calc(students[i].speed, students[i].distance);
      }
      students[i].distance /= work;
      students[i].speed /= work;
    }

    //?????????????°???¬?????°????±???????

    //?????????????????§?????????????????????????????????????????????????°???¬?????°????±???????
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        c_speed = students[i].speed;
      } else {
        if (c_speed >= students[i].speed) {
          work = calc(c_speed, students[i].speed);
          c_speed = ((c_speed / work) * students[i].speed);
        } else {
          work = calc(students[i].speed, c_speed);
          c_speed = ((c_speed / work) * students[i].speed);
        }
      }
    }

    //??????????????????????????????????????????
    for (int i = 0; i < n; i++) {
      students[i].distance *= (c_speed / students[i].speed);
    }

    //?¬?????????????????°???¬?????°????±???????
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        c_distance = students[i].distance;
      } else {
        if (c_distance >= students[i].distance) {
          work = calc(c_distance, students[i].distance);
          c_distance = ((c_distance / work) * students[i].distance);
        } else {
          work = calc(students[i].distance, c_distance);
          c_distance = ((c_distance / work) * students[i].distance);
        }
      }
    }

    for (int i = 0; i < n; i++) {
      students[i].distance = c_distance / students[i].distance;
    }

    //??????????????§??¬?´???°????±???????<?????????????????°??????>
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        work = students[i].distance;
      } else {
        if (work > students[i].distance) {
          work = calc(work, students[i].distance);
        } else {
          work = calc(students[i].distance, work);
        }
      }
    }

    //??????????´??????????
    for (int i = 0; i < n; i++) {
      students[i].distance /= work;
    }

    for (int i = 0; i < n; i++) {
      printf("%lld\\n", students[i].distance);
    }
  }

  return 0;
}