• pat1088. 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
    

    提交代码

    分情况讨论。其实代码可以写的更加简洁的,很多过程是重复的。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<stack>
      4 #include<algorithm>
      5 #include<iostream>
      6 #include<stack>
      7 #include<set>
      8 #include<map>
      9 #include<vector>
     10 using namespace std;
     11 long long gcd(long long a,long long b){//不能保证返回的符号,但能保证返回的绝对值大小
     12     if(b==0){
     13         if(a<0){
     14             a=-a;
     15         }
     16         return a;
     17     }
     18     return gcd(b,a%b);
     19 }
     20 long long com;
     21 void output(long long fz1,long long fm1){
     22     if(fz1==0){
     23         printf("0");
     24         return;
     25     }
     26     com=gcd(fz1,fm1);
     27     fz1/=com;
     28     fm1/=com;
     29 
     30     //cout<<fz1<<" "<<fm1<<" "<<com<<endl;
     31 
     32     if(fz1%fm1==0){
     33         printf("%lld",fz1/fm1);
     34     }
     35     else{
     36         if(fz1/fm1){
     37             printf("%lld ",fz1/fm1);
     38             if(fz1<0){
     39                 fz1=-fz1;
     40             }
     41         }
     42         printf("%lld/%lld",fz1-fz1/fm1*fm1,fm1);
     43     }
     44 }
     45 void add(long long fz1,long long fm1,long long fz2,long long fm2){
     46     if(fz1<0){
     47         printf("(");
     48         output(fz1,fm1);
     49         printf(")");
     50     }
     51     else{
     52         output(fz1,fm1);
     53     }
     54     printf(" + ");
     55     if(fz2<0){
     56         printf("(");
     57         output(fz2,fm2);
     58         printf(")");
     59     }
     60     else{
     61         output(fz2,fm2);
     62     }
     63     com=gcd(fm1,fm2);
     64     fz2*=fm1/com;
     65     fz1*=fm2/com;
     66     fm1*=fm2/com;
     67 
     68     //cout<<fz1<<"  "<<fm1<<"  "<<fz2<<"  "<<fm2<<endl;
     69 
     70 
     71     printf(" = ");
     72     fz1+=fz2;
     73     if(fz1<0){
     74         printf("(");
     75         output(fz1,fm1);
     76         printf(")");
     77     }
     78     else{
     79         output(fz1,fm1);
     80     }
     81 }
     82 void sub(long long fz1,long long fm1,long long fz2,long long fm2){
     83     if(fz1<0){
     84         printf("(");
     85         output(fz1,fm1);
     86         printf(")");
     87     }
     88     else{
     89         output(fz1,fm1);
     90     }
     91     printf(" - ");
     92     if(fz2<0){
     93         printf("(");
     94         output(fz2,fm2);
     95         printf(")");
     96     }
     97     else{
     98         output(fz2,fm2);
     99     }
    100     com=gcd(fm1,fm2);
    101     fz2*=fm1/com;
    102     fz1*=fm2/com;
    103     fm1*=fm2/com;
    104     printf(" = ");
    105     fz1-=fz2;
    106     if(fz1<0){
    107         printf("(");
    108         output(fz1,fm1);
    109         printf(")");
    110     }
    111     else{
    112         output(fz1,fm1);
    113     }
    114 }
    115 void mul(long long fz1,long long fm1,long long fz2,long long fm2){
    116     if(fz1<0){
    117         printf("(");
    118         output(fz1,fm1);
    119         printf(")");
    120     }
    121     else{
    122         output(fz1,fm1);
    123     }
    124     printf(" * ");
    125     if(fz2<0){
    126         printf("(");
    127         output(fz2,fm2);
    128         printf(")");
    129     }
    130     else{
    131         output(fz2,fm2);
    132     }
    133     printf(" = ");
    134     fz1*=fz2;
    135     fm1*=fm2;
    136     if(fz1<0){
    137         printf("(");
    138         output(fz1,fm1);
    139         printf(")");
    140     }
    141     else{
    142         output(fz1,fm1);
    143     }
    144 }
    145 void quo(long long fz1,long long fm1,long long fz2,long long fm2){
    146     if(fz1<0){
    147         printf("(");
    148         output(fz1,fm1);
    149         printf(")");
    150     }
    151     else{
    152         output(fz1,fm1);
    153     }
    154     printf(" / ");
    155     if(fz2<0){
    156         printf("(");
    157         output(fz2,fm2);
    158         printf(")");
    159     }
    160     else{
    161         output(fz2,fm2);
    162     }
    163     printf(" = ");
    164     fz1*=fm2;
    165     fm1*=fz2;
    166     if(fm1==0){
    167         printf("Inf");
    168         return;
    169     }
    170     if(fm1<0){
    171         fm1=-fm1;
    172         fz1=-fz1;
    173     }
    174     if(fz1<0){
    175         printf("(");
    176         output(fz1,fm1);
    177         printf(")");
    178     }
    179     else{
    180         output(fz1,fm1);
    181     }
    182 }
    183 int main()
    184 {
    185     //freopen("D:\INPUT.txt","r",stdin);
    186     long long fz1,fm1,inter1,fz2,fm2,inter2;
    187     scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2);
    188 
    189     //cout<<fm2<<endl;
    190 
    191     com=gcd(fz1,fm1);
    192     fz1/=com;
    193     fm1/=com;
    194     com=gcd(fz2,fm2);
    195 
    196     //cout<<com<<endl;
    197     //cout<<fm2<<endl;
    198     fz2/=com;
    199     fm2/=com;
    200     //cout<<fm2<<endl;
    201     //计算
    202     //cout<<fz1<<"  "<<fm1<<"  "<<fz2<<"  "<<fm2<<endl;
    203     add(fz1,fm1,fz2,fm2);
    204     printf("
    ");
    205     sub(fz1,fm1,fz2,fm2);
    206     printf("
    ");
    207     mul(fz1,fm1,fz2,fm2);
    208     printf("
    ");
    209     quo(fz1,fm1,fz2,fm2);
    210     printf("
    ");
    211     return 0;
    212 }
  • 相关阅读:
    关于使用AJAX获取数据时,由于IE缓存而导致数据不更新,串数据的问题!
    优化PHP代码的40条建议(转)
    这么晚了!难得今天看了几个小时的JAVA
    WEB开发,路漫漫其修远兮,个人的求索思考
    (转)高效的MySQL分页
    20121108随笔,关于代码严谨性、编写的优雅性
    Ubuntu 12.04LTS 安装PHP扩展pdo_oci.so支持ORACLE数据库
    程序员应该具备的11项基本技能
    Windows下配置使用MemCached
    PHP浮点数比较
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4785714.html
Copyright © 2020-2023  润新知