1. 程式人生 > >【PAT】1088. Rational Arithmetic (20)

【PAT】1088. Rational Arithmetic (20)

題目描述

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

翻譯:對於兩個分數,你的任務是進行基本運算,即計算它們的和、差、積、商。

INPUT FORMAT

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.

翻譯:每個輸入檔案包含一組測試資料,對於每組輸入資料,第一行包括”a1/b1 a2/b2”格式的兩個分數。分子和分母的範圍均為long int。如果有符號,則只會出現在分子前面。分母保證為非零數。

OUTPUT FORMAT

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.

翻譯:對於每組輸入資料,分別輸出四行兩個分數的加減乘除。每一行的格式為”number1 operator number2 = result”。注意所有分數必須化為最簡且為帶分數形式 “k a/b”, k為整數部分,a/b為最簡真分數。如果數字為負,則他必須被放在一對小括號內。如果除法中的被除數為0,則結果輸出“Inf”。資料保證所有輸出的整數在long int 範圍內。(乘法在化簡前可能為long 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

解題思路

這道題注意點比較多,所以程式碼寫的及其繁瑣雜亂,也懶得整理了,先就這樣吧,注意範圍可能到long long int。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>

using namespace std;
long long int a1,a2,b1,b2,I1,I2,I3,a3,b3; 
long long int gcd(long long int a,long long int b){
    if(a<0)a=-a;
    if(b<0)b=-b;
    return b?gcd(b,a%b):a;
}
void Tplus(){
    a3=a1*b2+a2*b1;
    b3=b1*b2;
    long long int temp=gcd(a3,b3);
    a3=a3/temp;
    b3=b3/temp;
    I3=a3/b3;a3=a3-I3*b3;
    long long int Ta1=a1-I1*b1,Ta2=a2-I2*b2;
    if(I1!=0){
        if(I1>0) printf("%lld ",I1);
        else printf("(%lld",I1);
    }
    if(Ta1!=0){
        if(Ta1<0&&I1>=0)printf("(%lld/%lld) ",Ta1,b1);
        else if(Ta1<0&&I1<0)printf(" %lld/%lld) ",-Ta1,b1);
        else printf("%lld/%lld ",Ta1,b1);
    }else{
        if(I1<0)printf(") ");
    }
    if(I1==0&&a1==0)printf("0 ");
    printf("+ ");
    if(I2!=0){
        if(I2>0) printf("%lld ",I2);
        else printf("(%lld",I2);
    }
    if(Ta2!=0){
        if(Ta2<0&&I2>=0)printf("(%lld/%lld) ",Ta2,b2);
        else if(Ta2<0&&I2<0)printf(" %lld/%lld) ",-Ta2,b2);
        else printf("%lld/%lld ",Ta2,b2);
    }else{
        if(I2<0)printf(") ");
    }
    if(I2==0&&a2==0)printf("0 ");
    printf("= ");
    if(I3!=0){
        if(I3>0) printf("%lld",I3);
        else printf("(%lld",I3);
    }
    if(a3!=0){
        if(a3<0&&I3>0)printf(" (%lld/%lld)",a3,b3);
        if(a3<0&&I3==0)printf("(%lld/%lld)",a3,b3);
        else if(a3<0&&I3<0)printf(" %lld/%lld)",-a3,b3);
        else if(I3==0) printf("%lld/%lld",a3,b3);
        else if(I3>0) printf(" %lld/%lld",a3,b3);
    }else{
        if(I3<0)printf(")");
    }
    if(I3==0&&a3==0)printf("0");
    printf("\n");
}
void Tminus(){
    a3=a1*b2-a2*b1;
    b3=b1*b2;
    long long int temp=gcd(a3,b3);
    a3=a3/temp;
    b3=b3/temp;
    I3=a3/b3;a3=a3-I3*b3;
    long long int Ta1=a1-I1*b1,Ta2=a2-I2*b2;
    if(I1!=0){
        if(I1>0) printf("%lld ",I1);
        else printf("(%lld",I1);
    }
    if(Ta1!=0){
        if(Ta1<0&&I1>=0)printf("(%lld/%lld) ",Ta1,b1);
        else if(Ta1<0&&I1<0)printf(" %lld/%lld) ",-Ta1,b1);
        else printf("%lld/%lld ",Ta1,b1);
    }else{
        if(I1<0)printf(") ");
    }
    if(I1==0&&a1==0)printf("0 ");
    printf("- ");
    if(I2!=0){
        if(I2>0) printf("%lld ",I2);
        else printf("(%lld",I2);
    }
    if(Ta2!=0){
        if(Ta2<0&&I2>=0)printf("(%lld/%lld) ",Ta2,b2);
        else if(Ta2<0&&I2<0)printf(" %lld/%lld) ",-Ta2,b2);
        else printf("%lld/%lld ",Ta2,b2);
    }else{
        if(I2<0)printf(") ");
    }
    if(I2==0&&a2==0)printf("0 ");
    printf("= ");
    if(I3!=0){
        if(I3>0) printf("%lld",I3);
        else printf("(%lld",I3);
    }
    if(a3!=0){
        if(a3<0&&I3>0)printf(" (%lld/%lld)",a3,b3);
        if(a3<0&&I3==0)printf("(%lld/%lld)",a3,b3);
        else if(a3<0&&I3<0)printf(" %lld/%lld)",-a3,b3);
        else if(I3==0) printf("%lld/%lld",a3,b3);
        else if(I3>0) printf(" %lld/%lld",a3,b3);
    }else{
        if(I3<0)printf(")");
    }
    if(I3==0&&a3==0)printf("0");
    printf("\n");
}
void Tmultiply(){
    a3=a1*a2;
    b3=b1*b2;
    long long int temp=gcd(a3,b3);
    a3=a3/temp;
    b3=b3/temp;
    I3=a3/b3;a3=a3-I3*b3;
    long long int Ta1=a1-I1*b1,Ta2=a2-I2*b2;
    if(I1!=0){
        if(I1>0) printf("%lld ",I1);
        else printf("(%lld",I1);
    }
    if(Ta1!=0){
        if(Ta1<0&&I1>=0)printf("(%lld/%lld) ",Ta1,b1);
        else if(Ta1<0&&I1<0)printf(" %lld/%lld) ",-Ta1,b1);
        else printf("%lld/%lld ",Ta1,b1);
    }else{
        if(I1<0)printf(") ");
    }
    if(I1==0&&a1==0)printf("0 ");
    printf("* ");
    if(I2!=0){
        if(I2>0) printf("%lld ",I2);
        else printf("(%lld",I2);
    }
    if(Ta2!=0){
        if(Ta2<0&&I2>=0)printf("(%lld/%lld) ",Ta2,b2);
        else if(Ta2<0&&I2<0)printf(" %lld/%lld) ",-Ta2,b2);
        else printf("%lld/%lld ",Ta2,b2);
    }else{
        if(I2<0)printf(") ");
    }
    if(I2==0&&a2==0)printf("0 ");
    printf("= ");
    if(I3!=0){
        if(I3>0) printf("%lld",I3);
        else printf("(%lld",I3);
    }
    if(a3!=0){
        if(a3<0&&I3>0)printf(" (%lld/%lld)",a3,b3);
        if(a3<0&&I3==0)printf("(%lld/%lld)",a3,b3);
        else if(a3<0&&I3<0)printf(" %lld/%lld)",-a3,b3);
        else if(I3==0) printf("%lld/%lld",a3,b3);
        else if(I3>0) printf(" %lld/%lld",a3,b3);
    }else{
        if(I3<0)printf(")");
    }
    if(I3==0&&a3==0)printf("0");
    printf("\n");
}
void Tdivide(){
    a3=a1*b2;
    b3=b1*a2;
    if(b3<0){
        a3=-a3;b3=-b3; 
    } 
    long long int temp=gcd(a3,b3);
    a3=a3/temp;
    b3=b3/temp;
    if(b3!=0){
        I3=a3/b3;a3=a3-I3*b3;
    }else I3=0;
    long long int Ta1=a1-I1*b1,Ta2=a2-I2*b2;
        if(I1!=0){
        if(I1>0) printf("%lld ",I1);
        else printf("(%lld",I1);
    }
    if(Ta1!=0){
        if(Ta1<0&&I1>=0)printf("(%lld/%lld) ",Ta1,b1);
        else if(Ta1<0&&I1<0)printf(" %lld/%lld) ",-Ta1,b1);
        else printf("%lld/%lld ",Ta1,b1);
    }else{
        if(I1<0)printf(") ");
    }
    if(I1==0&&a1==0)printf("0 ");
    printf("/ ");
    if(I2!=0){
        if(I2>0) printf("%lld ",I2);
        else printf("(%lld",I2);
    }
    if(Ta2!=0){
        if(Ta2<0&&I2>=0)printf("(%lld/%lld) ",Ta2,b2);
        else if(Ta2<0&&I2<0)printf(" %lld/%lld) ",-Ta2,b2);
        else printf("%lld/%lld ",Ta2,b2);
    }else{
        if(I2<0)printf(") ");
    }
    if(I2==0&&a2==0)printf("0 ");
    printf("= ");
    if(I3!=0){
        if(I3>0) printf("%lld",I3);
        else printf("(%lld",I3);
    }
    if(a3!=0&&b3!=0){
        if(a3<0&&I3>0)printf(" (%lld/%lld)",a3,b3);
        if(a3<0&&I3==0)printf("(%lld/%lld)",a3,b3);
        else if(a3<0&&I3<0)printf(" %lld/%lld)",-a3,b3);
        else if(I3==0) printf("%lld/%lld",a3,b3);
        else if(I3>0) printf(" %lld/%lld",a3,b3);
    }else{
        if(I3<0)printf(")");
    }
    if(b3==0)printf("Inf");
    if(I3==0&&a3==0&&b3!=0)printf("0");
    printf("\n");
}
int main(){
    scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
    long long int temp1=gcd(a1,b1),temp2=gcd(a2,b2);
    a1=a1/temp1;
    b1=b1/temp1;
    a2=a2/temp2;
    b2=b2/temp2;    
    I1=a1/b1;
    I2=a2/b2;
    Tplus();
    Tminus();
    Tmultiply();
    Tdivide();
    exit(0);
}