您的位置:首页 > 其它

1088. Rational Arithmetic (20)

2015-08-16 16:08 330 查看
题目地址 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

水题一道,注意细节(数据范围,/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 100001

long 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: