1034. 有理数四则运算(20)
时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。
输出格式:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。
输入样例1:2/3 -4/2输出样例1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)输入样例2:
5/3 0/6输出样例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
题目理解起来很简单,只是处理有点复杂,要考虑各种情况,我被坑了两次,第一次是没有用long long,虽然题目说最后结果在int范围内,但是计算过程两数相乘时可能会超出int范围,索性全改为long long,第二次是符号没考虑周全,分子分母都为负时没有考虑到结果输出两个负号。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 long long gcd( long long a, long long b)
6 {
7 long long temp;
8 while (a%b)
9 {
10 temp=a%b;
11 a=b;
12 b=temp;
13 }
14 if (b> 0)
15 return b;
16 else
17 return -b;
18 }
19 void out( long long a, long long b)
20 {
21 if (a> 0&&b< 0)
22 {
23 a=-a;
24 b=-b;
25 }
26 if (a< 0&&b< 0)
27 {
28 a=-a;
29 b=-b;
30 }
31 if (b== 0)
32 {
33 cout << " Inf ";
34 return;
35 }
36 long long t=gcd(a,b);
37 a=a/t;
38 b=b/t;
39 long long c=a/b;
40 if (b== 1)
41 {
42 if (a< 0)
43 cout << " ( "<<a<< " ) ";
44 else
45 cout <<a;
46 }
47 else
48 {
49 if (a< 0)
50 {
51 cout << " ( ";
52 if(c!= 0)
53 {
54 cout <<c<< " ";
55 if (a< 0)
56 a=-a;
57 a=a%b;
58 }
59 cout <<a<< " / "<<b<< " ) ";
60 }
61 else
62 {
63 if(c!= 0)
64 {
65 cout <<c<< " ";
66 if (a< 0)
67 a=-a;
68 a=a%b;
69 }
70 cout <<a<< " / "<<b;
71 }
72 }
73 return;
74 }
75 int main()
76 {
77 long long a1,b1,a2,b2,a,c;
78 char ch;
79 while (cin>>a1>>ch>>b1)
80 {
81 cin>>a2>>ch>>b2;
82
83 out(a1,b1);
84 cout << " + ";
85 out(a2,b2);
86 cout << " = ";
87 out(a1*b2+b1*a2,b1*b2);
88 cout <<endl;
89
90 out(a1,b1);
91 cout << " - ";
92 out(a2,b2);
93 cout << " = ";
94 out(a1*b2-b1*a2,b1*b2);
95 cout <<endl;
96
97 out(a1,b1);
98 cout << " * ";
99 out(a2,b2);
100 cout << " = ";
101 out(a1*a2,b1*b2);
102 cout <<endl;
103
104 out(a1,b1);
105 cout << " / ";
106 out(a2,b2);
107 cout << " = ";
108 out(a1*b2,b1*a2);
109 cout <<endl;
110 }
111 return 0;
112 }
2 #include <cstring>
3 using namespace std;
4
5 long long gcd( long long a, long long b)
6 {
7 long long temp;
8 while (a%b)
9 {
10 temp=a%b;
11 a=b;
12 b=temp;
13 }
14 if (b> 0)
15 return b;
16 else
17 return -b;
18 }
19 void out( long long a, long long b)
20 {
21 if (a> 0&&b< 0)
22 {
23 a=-a;
24 b=-b;
25 }
26 if (a< 0&&b< 0)
27 {
28 a=-a;
29 b=-b;
30 }
31 if (b== 0)
32 {
33 cout << " Inf ";
34 return;
35 }
36 long long t=gcd(a,b);
37 a=a/t;
38 b=b/t;
39 long long c=a/b;
40 if (b== 1)
41 {
42 if (a< 0)
43 cout << " ( "<<a<< " ) ";
44 else
45 cout <<a;
46 }
47 else
48 {
49 if (a< 0)
50 {
51 cout << " ( ";
52 if(c!= 0)
53 {
54 cout <<c<< " ";
55 if (a< 0)
56 a=-a;
57 a=a%b;
58 }
59 cout <<a<< " / "<<b<< " ) ";
60 }
61 else
62 {
63 if(c!= 0)
64 {
65 cout <<c<< " ";
66 if (a< 0)
67 a=-a;
68 a=a%b;
69 }
70 cout <<a<< " / "<<b;
71 }
72 }
73 return;
74 }
75 int main()
76 {
77 long long a1,b1,a2,b2,a,c;
78 char ch;
79 while (cin>>a1>>ch>>b1)
80 {
81 cin>>a2>>ch>>b2;
82
83 out(a1,b1);
84 cout << " + ";
85 out(a2,b2);
86 cout << " = ";
87 out(a1*b2+b1*a2,b1*b2);
88 cout <<endl;
89
90 out(a1,b1);
91 cout << " - ";
92 out(a2,b2);
93 cout << " = ";
94 out(a1*b2-b1*a2,b1*b2);
95 cout <<endl;
96
97 out(a1,b1);
98 cout << " * ";
99 out(a2,b2);
100 cout << " = ";
101 out(a1*a2,b1*b2);
102 cout <<endl;
103
104 out(a1,b1);
105 cout << " / ";
106 out(a2,b2);
107 cout << " = ";
108 out(a1*b2,b1*a2);
109 cout <<endl;
110 }
111 return 0;
112 }
程序写的少,代码质量不好,敬请谅解!