puzzle contents
Contents
raw puzzle

Original Problem

Goal

Given a string representing a simple fraction X/Y, you must output a string representing the corresponding mixed fraction in the following format:

[sign]A B/C

where A is integer part and B/C is irreducible proper fraction. There must be exactly one space between A and B/C.

Some rules must be followed:

1. If X/Y equals the integer part, return integer part only (and ignore Rule 2).
2. If integer part is zero, return the irreducible proper fraction only.
3. In cases where rules (1) or (2) apply, the result must not contain any spaces.
4. In case of division by zero, output DIVISION BY ZERO.
5. The sign of the resulting number applies to this number as a whole and if the sign is negative, it is placed before the number without a space after the sign. Zero does not have a sign.

Examples:

Input: 42/9, expected result: 4 2/3.
Input: 6/3, expected result: 2.
Input: 4/6, expected result: 2/3.
Input: 0/18891, expected result: 0.
Input: -10/7, expected result: -1 3/7.
Inputs 0/0 or 3/0 must output DIVISION BY ZERO.
Input
Number of tests N.
Next N lines: Two integers separated by a slash: X/Y.
Output
The resulting mixed number, one per line.
Constraints
-10000000 < X < 10000000
-10000000 < Y < 10000000

Solution

Given the rules, we can go straight to work and transfer them to four caes:

Case 1) \(Y = 0\): If the denominator is zero, we will run into a division by zero and stop the calculation.

Case 2) \(Y | X\): If the denominator divides the numerator, we can divide and Y by X and are done.

Case 3) \(A\neq 0\): If the integer part is not zero, we can print it and append the reduced fractional part.

Case 4) \(A= 0\): The integer part is zero and must be omitted. Only the fractional part is getting printed.

When we implement this procedure, we need to calculate the greatest common denominator to reduce the fractional part, which can be done in one line and then simply write down the cases:

var gcd = (a, b) => b === 0 ? a : gcd(b, a % b);

var N = +readline();
for (var i = 0; i < N; i++) {

  var s = "";

  var [x, y] = readline().split("/").map(Number);

  if (y === 0) {
    s = 'DIVISION BY ZERO';
  } else if (x === 0) {
    s = 0;
  } else {

    if (y < 0) {
      x = -x;
      y = -y;
    }
    if (x < 0) {
      x = -x;
      s = "-";
    }
    if (x >= y) {
      s += x / y | 0;
      x %= y;
      if (x > 0) {
        s += " ";
      }
    }

    if (x > 0) {
      var g = gcd(x, y);
      s += (x / g) + "/" + (y / g);
    }
  }
  print(s);
}