-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB1034.cpp
More file actions
67 lines (59 loc) · 1.1 KB
/
Copy pathB1034.cpp
File metadata and controls
67 lines (59 loc) · 1.1 KB
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
#include <cstdio>
#include <cmath>
using namespace std;
int gcd(int a, int b)
{
return (b == 0)? a:gcd(b, a%b);
}
void print0(int a, int b)
{
if(gcd(a,b) != 1)
{
a = a / gcd(a, b);
b = b / gcd(a, b);
}
if(abs(a) > abs(b) && a%b != 0)
printf("%d %d/%d", a/b, (a - a/b * b), b);
else if(a%b == 0)
printf("%d", a/b);
else
printf("%d/%d", a, b);
}
int main()
{
int a1, b1, a2, b2;
scanf("%d/%d %d/%d", &a1, &b1, &a2, &b2);
//printf("%d %d %d %d\n", a1, b1, a2, b2);
int down, up1, up2;
down = b1 * b2 / gcd(b1, b2);
up1 = a1 * down / b1;
up2 = a2 * down / b2;
print0(a1, b1);
printf(" + ");
print0(a2, b2);
printf(" = ");
print0(up1+up2, down);
printf("\n");
print0(a1, b1);
printf(" - ");
print0(a2, b2);
printf(" = ");
print0(up1-up2, down);
printf("\n");
print0(a1, b1);
printf(" * ");
print0(a2, b2);
printf(" = ");
print0(a1 * a2, b1 * b2);
printf("\n");
print0(a1, b1);
printf(" / ");
print0(a2, b2);
printf(" = ");
if(a2 != 0)
print0(a1 * b2, b1 * a2);
else
printf("Inf");
printf("\n");
return 0;
}