浙江财经大学
信息管理与工程学院

PAT(A) 1088. Rational Arithmetic (20)

本文由 Ocrosoft 于 2016-12-28 21:54:30 发表

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
分数加减乘除

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
#define  rep(a,v,b) for(int a=v;a<b;a++)
#define  repe(a,v,b) for(int a=v;a<=b;a++)
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 300000 + 10;
const int MOD = 1000000009;
LL GCD(LL a, LL b)
{
	if (!b)return a;
	return GCD(b, a%b);
}
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
/*(◕‿‿◕) 签订契约,成为马猴烧酒吧! (◕‿‿◕)*/
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
using namespace std;
struct rational
{
	LL a, b;
	void adj()
	{
		LL gcd = GCD(abs(a), b);
		a /= gcd; b /= gcd;
	}
	void add(rational other)
	{
		LL lcm = b*other.b / GCD(b, other.b);
		a *= lcm / b;
		a += other.a*(lcm / other.b);
		b = lcm;
		LL gcd = GCD(abs(a), b);
		a /= gcd, b /= gcd;
	}
	void sub(rational other)
	{
		rational tmp = other;
		tmp.a = -tmp.a;
		add(tmp);
	}
	void pro(rational other)
	{
		rational tmp = other;
		LL gcd = GCD(abs(a), other.b);
		a /= gcd; tmp.b /= gcd;
		gcd = GCD(b, abs(other.a));
		b /= gcd; tmp.a /= gcd;
		a *= tmp.a; b *= tmp.b;
		gcd = GCD(abs(a), b);
		a /= gcd; b /= gcd;
	}
	void quo(rational other)
	{
		rational tmp = other;
		if (!tmp.a || !tmp.b) { b = 0; return; }//b==0表示inf
		swap(tmp.a, tmp.b);
		if (tmp.b < 0)tmp.a = -tmp.a, tmp.b = -tmp.b;
		pro(tmp);
	}
	void print()
	{
		if (!b) { printf("Inf"); return; }
		if (!a) { printf("0"); return; }
		if (a < 0)printf("(");
		if (abs(a) >= b)
		{
			printf("%lld", a / b);
			if (a%b)printf(" %lld/%lld", abs(a%b), b);
		}
		else printf("%lld/%lld", a, b);
		if (a < 0)printf(")");
	}
};
int main()
{
	rational x, y, tmp;
	scanf("%lld/%lld", &x.a, &x.b); x.adj();
	scanf("%lld/%lld", &y.a, &y.b); y.adj();
	x.print(); printf(" + "); y.print(); tmp = x; tmp.add(y); printf(" = "); tmp.print(); printf("\n");
	x.print(); printf(" - "); y.print(); tmp = x; tmp.sub(y); printf(" = "); tmp.print(); printf("\n");
	x.print(); printf(" * "); y.print(); tmp = x; tmp.pro(y); printf(" = "); tmp.print(); printf("\n");
	x.print(); printf(" / "); y.print(); tmp = x; tmp.quo(y); printf(" = "); tmp.print(); printf("\n");
	return 0;
}

欢迎转载,请保留出处与链接。Ocrosoft » PAT(A) 1088. Rational Arithmetic (20)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址