题目

2/3 -4/2

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

5/3 0/6

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

AC代码

//模拟 + 代码化

/*

float point expection -> 不能除0
mle or segmentation fault -> gcd传参不能一正一负，正数和负数是没有最大公因数的，同号之间才有最大公约数 -> gcd 传参是一定要都是正整数

1.
-0/7 7/3
2.
0/5 0/8  除法爆
3.
12/10 15/12  第二个测试点

12/10 15/12
1 2/10 + 1 3/12 = 2 9/20
1 2/10 - 1 3/12 = (-1/20)
1 2/10 * 1 3/12 = 1 1/2
1 2/10 / 1 3/12 = 24/25

1 1/5 + 1 1/4 = 2 9/20
1 1/5 - 1 1/4 = (-1/20)
1 1/5 * 1 1/4 = 1 1/2
1 1/5 / 1 1/4 = 24/25
*/

/*
0 对任何数取模都等于 0

0与0的最大公约数是0
*/

#include <iostream>
#include <cmath>

using namespace std;

typedef long long ll;

string a, b;
ll a1, a2, b1, b2, fz, fm;

/*

5 -3
-3 2 ->2 -3
-3 2 ->2 -3
-3 2
... 死循环

** 一个整数a，一个整数b
** if abs(a) < abs(b) -> a % b = a;
*/
inline ll gcd (ll a, ll b)
{
if(b > a) return gcd(b, a);

return b == 0 ? a : gcd(b,a % b);
}

inline ll lcm (ll a, ll b)
{
return a * b / gcd(a, b);
}

inline void out (char c)
{
if(a1 < 0) printf("(");

if(a1 == 0) printf("0");
else if(abs(a1) < abs(a2)) printf("%lld/%lld", a1, a2);
else if(a1 % a2 == 0)  printf("%lld", a1 / a2);
else if(a1 % a2 != 0) printf("%lld %lld/%lld", a1 / a2, abs(a1) % a2, a2);

if(a1 < 0) printf(")");

printf(" %c ", c);

if(b1 < 0) printf("(");

if(b1 == 0) printf("0");
else if(abs(b1) < abs(b2)) printf("%lld/%lld", b1, b2);
else if(b1 % b2 == 0)  printf("%lld", b1 / b2);
else if(b1 % b2 != 0) printf("%lld %lld/%lld", b1 / b2, abs(b1) % b2, b2);

if(b1 < 0) printf(")");

printf(" = ");

if(fz < 0) printf("(");

if(fz == 0) printf("0"); //0/8 15/4
else if(fm == 0) printf("Inf"); //5/3 0/6
else if(abs(fz) < abs(fm)) printf("%lld/%lld", fz, fm);  //3/4
else if(fz % fm == 0)  printf("%lld", fz / fm); //-4 2
else if(fz % fm != 0) printf("%lld %lld/%lld", fz / fm, abs(fz) % fm, fm); //-5 3

if(fz < 0) printf(")");

printf("\n");
}

{
ll gbs = lcm(a2, b2);

ll gys = gcd(abs((gbs / a2) * a1 + (gbs / b2) * b1), gbs);

fz = ((gbs / a2) * a1 + (gbs / b2) * b1) / gys;
fm = gbs / gys;

out('+');
}

inline void sub ()
{
ll gbs = lcm(a2, b2);

ll gys = gcd(abs((gbs / a2) * a1 - (gbs / b2) * b1), gbs); //一个正数和一个负数没有公约数

fz = ((gbs / a2) * a1 - (gbs / b2) * b1) / gys;
fm = gbs / gys;

out('-');
}

inline void mul ()
{
fz = a1 * b1;
fm = a2 * b2;

ll gys = gcd(abs(fz), abs(fm));
fz = fz / gys;
fm = fm / gys;

out('*');
}

inline void div ()
{
fz = a1 * b2;
fm = a2 * b1;
//cout<<fz<<" "<<fm<<" "<<endl;
if(fz == 0 && fm == 0) //0/8 0/9
{
printf("0 / 0 = Inf\n");
return ;
}

ll gys = gcd(abs(fz), abs(fm));
fz /= gys;
fm /= gys;

if(fm < 0) {fm = -fm; fz = -fz;}

out('/');
}

int main ()
{
//***
scanf("%lld/%lld %lld/%lld", &a1, &a2, &b1, &b2);

//输入的分数也要约分输出 测试点2
ll gys_a = gcd(abs(a1), abs(a2));
a1 /= gys_a;
a2 /= gys_a;
ll gys_b = gcd(abs(b1), abs(b2));
b1 /= gys_b;
b2 /= gys_b;

sub();
mul();
div();

return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

呵呵的先生

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-26
02-17 186

11-11 478
11-17 65
07-18 43
03-23 40
01-07 1189
10-20
10-17 7072