天天看點

PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)

1088. Rational Arithmetic (20)

時間限制

200 ms

記憶體限制

65536 kB

代碼長度限制

16000 B

判題程式

Standard

作者

CHEN, Yue

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)      

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf      

模拟分數的四則運算,細節多。

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
LL x, y, z, a, b, c;
char s[maxn];

void write(LL x, LL y, LL z)
{
  if (x&&y) printf("(-");
  if (y / z) printf("%lld", y / z);
  if (y / z && y % z) printf(" ");
  if (y % z) printf("%lld/%lld", y % z, z);
  if (!(y / z) && !(y % z)) printf("0");
  if (x&&y) printf(")");
}

LL gcd(LL x, LL y)
{
  return x%y ? gcd(y, x%y) : y;
}

void get(LL &x, LL &y)
{
  if (!x) { y = 1; return; }
  LL g = gcd(x, y);
  x /= g;
  y /= g;
}

void add(LL a, LL b, LL c, LL x, LL y, LL z)
{
  LL g = gcd(c, z);
  LL gg = c / g * z;
  b = z / g * b;
  y = c / g * y;
  if (a == x) b += y;
  else if (!a)
  {
    if (b >= y) b -= y; else a = 1, b = y - b;
  }
  else
  {
    if (b > y) b -= y; else a = 0, b = y - b;
  }
  g = gcd(b, gg);
  b /= g; c = gg / g;
  write(a, b, c);
}

int main()
{
  scanf("%s", s);
  a = s[0] == '-';
  sscanf(s + a, "%lld/%lld", &b, &c);
  get(b, c);

  scanf("%s", s);
  x = s[0] == '-';
  sscanf(s + x, "%lld/%lld", &y, &z);
  get(y, z);

  write(a, b, c); printf(" + "); write(x, y, z); printf(" = ");
  add(a, b, c, x, y, z);  printf("\n");

  write(a, b, c); printf(" - "); write(x, y, z); printf(" = ");
  add(a, b, c, x^1, y, z);  printf("\n");

  write(a, b, c); printf(" * "); write(x, y, z); printf(" = ");
  LL g = gcd(b*y, c*z); write(a^x, b*y / g, c*z / g);
  printf("\n");

  write(a, b, c); printf(" / "); write(x, y, z); printf(" = ");
  if (y == 0) printf("Inf"); else
  {
    LL g = gcd(b*z, c*y); write(a^x, b*z / g, c*y / g);
  }
  printf("\n");
  return 0;
}