PAT-A 真题 – 1088 Rational Arithmetic

发布于 / PAT-甲级 / 0 条评论

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

题目大意:给定两个分数,做加减乘除四则运算。

要求负数加括号,化成最简真分数;如果遇到处以0的情况,输出Inf(测试点1,第二个测试点是Inf。注意第一个I是大写的,否则扣5分)

这道题比较简单的方法是直接同分运算,然后化成最简真分数。代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

//递归求最小公约数 
long long gcd(long long t1, long long t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
//fuck日分数的方法 
void fuck(long long a, long long b){
  if(!b){  //如果b=0,输出Inf并返回 
    printf("Inf");
    return;
  }
  //判断符号,true为+,false为- 
  bool sig = (a < 0 && b < 0) || (a > 0 && b > 0) ? true : false;
  //求最小公约数,用于约分 
  long long g = gcd(a, b);
  a /= g;  //约分 
  b /= g;  //约分 
  a = abs(a), b = abs(b);  //把他们都化成正数,方便后续计算 
  long long num = a / b;
  a -= num * b;  //把数字部分从分子中去掉 
  if(!num && !a){  //如果num和a都是0,直接输出0 
    printf("0");
    return;  
  }
  if(!sig) printf("(-");  //如果是负数,输出括号并输出符号 
  if(num){      //如果num非0,输出num和空格,num就是那个“又” 
    printf("%d", num);
    if(a) printf(" ");  //如果a非0,说明后面还有分数,输出一个空格 
  }
  if(a) printf("%lld/%lld", a, b);  //如果分子非0,输出分数 
  if(!sig) printf(")");        //如果负号,输出括号的另一半 
}
 
int main(){
  long long a, b, c, d;  //||| a/b, c/d
  scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
  //加 
  fuck(a,b); printf(" + "); fuck(c,d); printf(" = "); fuck(a*d+c*b,b*d);//||| (ad+bc)/bd;
  printf("\n");
  //减 
  fuck(a,b); printf(" - "); fuck(c,d); printf(" = "); fuck(a*d-c*b,b*d);//||| (ad-bc)/bd;
  printf("\n");
  //乘 
  fuck(a,b); printf(" * "); fuck(c,d); printf(" = "); fuck(a*c,b*d);    //ac/bd;
  printf("\n");
  //除 
  fuck(a,b); printf(" / "); fuck(c,d); printf(" = "); fuck(a*d,b*c);    //ad/bc;
  printf("\n");
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题 – 1088 Rational Arithmetic
目前还没有评论,快来抢沙发吧~