1088. Rational Arithmetic (20) -- 公约数,字符串输出处理

来源:互联网 发布:windows c 串口编程 编辑:程序博客网 时间:2024/05/27 21:08

题目地址

http://www.patest.cn/contests/pat-a-practise/1088

题目描述

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

ac1

  • 水题一道,注意细节(数据范围,/0等),多验证几个就行了
#include <stdio.h>#include <iostream>  #include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <math.h>#include <map>#include <unordered_map>#include <queue>#include <vector>#include <set>#include <iterator>using namespace std;#define N 100001long int a1, b1, a2, b2;long int gcd(long int m, long int n){  if (m < n)  {    long int tmp = m;    m = n;    n = tmp;  }  long int r = m % n;  while (r != 0)  {    m = n;    n = r;    r = m % n;  }  return n;}void pf(long int a1, long int b1){  if (a1 == 0)  {    printf("0");    return;  }  int flag = 1;  if (a1 < 0)  {    flag = -1;    a1 = -a1;  }  long int gcdd = gcd(a1, b1);  a1 /= gcdd;  b1 /= gcdd;  if (flag == -1)  {    printf("(-");  }  if (a1 >= b1)  {    long int tmp = a1 / b1;    a1 = a1 % b1;    printf("%ld", tmp);    if (a1 > 0)    {      printf(" %ld/%ld", a1, b1);    }    else{    }  }  else{    printf("%ld/%ld", a1, b1);  }  if (flag == -1)  {    printf(")");  }}void add(){  long int aa = a1 * b2 + a2*b1;  long int bb = b1 * b2;  pf(aa, bb);}void sub(){  long int aa = a1 * b2 - a2*b1;  long int bb = b1 * b2;  pf(aa, bb);}void cheng(){  long int aa = a1*a2;  long int bb = b1 * b2;  pf(aa, bb);}void chu(){  if (a2 == 0)  {    printf("Inf");  }  else if (a2 < 0){    a1 = -a1;    a2 = -a2;    long int aa = a1*b2;    long int bb = b1 * a2;    pf(aa, bb);  }  else{    long int aa = a1*b2;    long int bb = b1 * a2;    pf(aa, bb);  }}int main(){  //freopen("in", "r", stdin);  while (scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2) != EOF)  {    pf(a1, b1);    printf(" + ");    pf(a2, b2);    printf(" = ");    add();    printf("\n");    pf(a1, b1);    printf(" - ");    pf(a2, b2);    printf(" = ");    sub();    printf("\n");    pf(a1, b1);    printf(" * ");    pf(a2, b2);    printf(" = ");    cheng();    printf("\n");    pf(a1, b1);    printf(" / ");    pf(a2, b2);    printf(" = ");    chu();    printf("\n");  }  return 0;}

ac2

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> #include <unordered_map>using namespace std;#define INF 0x7ffffffftypedef long long int LL;LL a1, b1, a2, b2;LL gongyue(LL n ,LL m){    if(m < n)    {        LL tmp = n;        n = m;        m = tmp;    }    LL r = m % n;    while(r != 0)    {        m = n;        n = r;        r = m % n;    }    return n;}void jianhua(LL &a, LL &b){    if(a == 0){        b = 1;    }else if(a > 0)    {           LL tmp = gongyue(a,b);        a /= tmp;        b /= tmp;    }else{        LL tmp = gongyue(-a,b);        a /= tmp;        b /= tmp;    }}void show(LL a, LL b){    if(a == 0)    {        printf("0");    }else if(b == 1){        if(a > 0)            printf("%lld", a);        else            printf("(%lld)", a);    }else{        if(a > 0)        {            if(a > b)            {                printf("%lld", a/b);                printf(" %lld/%lld", a % b, b);            }else{                printf("%lld/%lld", a, b);            }        }else{            printf("(-");            a = -a;            if(a > b)            {                printf("%lld", a/b);                printf(" %lld/%lld", a % b, b);            }else{                printf("%lld/%lld", a, b);            }            printf(")");        }    }}void showAns(LL a, LL b,char sign){    show(a1, b1);    if(sign == '+'){        printf(" + ");    }else if(sign == '-'){        printf(" - ");    }else if(sign == '*'){        printf(" * ");    }else{        printf(" / ");    }    show(a2, b2);    printf(" = ");    show(a, b);    printf("\n");}void add(LL &a, LL &b){    LL tmp = gongyue(b1, b2);    LL fenmu = b1 * b2 / tmp;    LL fenzi = a1 * fenmu / b1 + a2 * fenmu / b2;    jianhua(fenzi,fenmu);    a = fenzi;    b = fenmu;}void sub(LL & a, LL &b){    LL tmp = gongyue(b1, b2);    LL fenmu = b1 * b2 / tmp;    LL fenzi = a1 * fenmu / b1 - a2 * fenmu / b2;    jianhua(fenzi,fenmu);    a = fenzi;    b = fenmu;}void multi(LL & a, LL &b){    if(a1 == 0 || a2 == 0)    {        a = 0;        b = 1;        return;    }    a = a1 * a2;    b = b1 * b2;    jianhua(a,b);}void chu(LL &a, LL &b){    bool f1 = true;    bool f2 = true;    LL tmpa1 = a1;    if(tmpa1  < 0){        tmpa1  = -tmpa1 ;        f1 = false;    }    LL tmpa2 = a2;    if(tmpa2 < 0)    {        tmpa2 = -tmpa2;        f2 = false;    }    bool flag = (f1 == f2);    b = tmpa2 * b1;    a = tmpa1 * b2;    if(flag == false)        a = -a;    jianhua(a,b);}int main( ){       //freopen("in.txt", "r", stdin);    while( scanf("%lld/%lld %lld/%lld", &a1,&b1,&a2,&b2) != EOF)    {        jianhua(a1,b1);        jianhua(a2,b2);        LL a, b;        add(a,b);        showAns(a, b, '+');        sub(a,b);        showAns(a, b, '-');        multi(a,b);        showAns(a, b, '*');        if(a2 == 0)        {            show(a1, b1);            printf(" / ");            show(a2, b2);            printf(" = Inf");            printf("\n");        }else if(a1 == 0){            show(a1, b1);            printf(" / ");            show(a2, b2);            printf(" = 0");            printf("\n");        }else{            chu(a, b);            showAns(a, b, '/');        }        //printf("\n");    }    return 0;}
0 0
原创粉丝点击