A1088 Rational Arithmetic (20 point(s))

分数计算及假分数的表示

1. 原文

原题

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:

1
2/3 -4/2

Sample output 1:

1
2
3
4
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:

1
5/3 0/6

Sample output 2:

1
2
3
4
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

2. 解析

  • 分数初始化

    init(node &f) 求最大公约数后简化

  • 计算

    分数计算

  • 表示

    print(node f)

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<cstdio>
#include<algorithm>
using namespace std;
long long gcd(long long a,long long b){
if (b==0)
{
return a;
}else{
return gcd(b,a%b);
}
}
struct node{
long long up,down;
};
void init(node &f){
if (f.down<0)
{
f.up=-f.up;
f.down=-f.down;
}
if (f.up==0)
{
f.down=1;
}else{
long long d=gcd(abs(f.up),abs(f.down));
f.up/=d;
f.down/=d;
}
}
void print(node f){
if (f.up<0)
{
printf("(");
}
if (f.down==1)
{
printf("%lld",f.up);
}else if(abs(f.up)>f.down){
printf("%lld %lld/%lld",f.up/f.down,abs(f.up)%f.down,f.down);
}else{
printf("%lld/%lld",f.up,f.down);
}
if (f.up<0)
{
printf(")");
}
}
int main()
{
node f1,f2;
scanf("%lld/%lld %lld/%lld",&f1.up,&f1.down,&f2.up,&f2.down);
node result;
//+
init(f1); init(f2);
result.up=f1.up*f2.down+f2.up*f1.down;
result.down=f1.down*f2.down;
init(result);
print(f1);printf(" + ");print(f2);printf(" = ");print(result);printf("\n");
//-
result.up=f1.up*f2.down-f2.up*f1.down;
result.down=f1.down*f2.down;
init(result);
print(f1);printf(" - ");print(f2);printf(" = ");print(result);printf("\n");
//*
result.up=f1.up*f2.up;
result.down=f1.down*f2.down;
init(result);
print(f1);printf(" * ");print(f2);printf(" = ");print(result);printf("\n");
///
print(f1);printf(" / ");print(f2);printf(" = ");
if (f2.up==0)
{
printf("Inf\n");
}else{
result.up=f1.up*f2.down;
result.down=f1.down*f2.up;
init(result);
print(result);printf("\n");
}

return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1088/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!