[PAT] A1088 Rational Arithmetic

题目大意

给出两个分数,求加减乘除的结果。

思路

求最大公约数要用辗转相除法,否则会超时。
判断符号不能两数相乘,因为乘积可能超过long long范围。

tips

题目说了输入格式一定是a1/b1 a2/b2,所以直接输入即可,不用字符串来转(在这里耗了好多时间和代码...555...我真傻,真的。)

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
void huajian(long long int fenzi, long long int fenmu) {
	if (fenmu == 0) {
		printf("Inf"); return;
	}
	if (fenzi == 0) {
		printf("0"); return;
	}
	bool fuhao = 1;
	if ((fenzi < 0 && fenmu > 0) || (fenzi > 0 && fenmu < 0))fuhao = 0;
	if (fuhao == false)printf("(-");
	if (abs(fenzi) >= abs(fenmu)) {//假分数,先求出整数部分
		printf("%lld", abs(fenzi / fenmu));
		fenzi = fenzi % fenmu;
		if (fenzi != 0)printf(" ");
	}
	if (fenzi != 0) {
		long long k = gcd(abs(fenzi), abs(fenmu));
		fenzi = fenzi / k;fenmu = fenmu / k;
		printf("%lld/%lld", abs(fenzi), abs(fenmu));
	}
	if (fuhao == false)printf(")");
}
int main() {
	long long int a1, b1, a2, b2;
	scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
	huajian(a1, b1);printf(" + ");huajian(a2, b2);printf(" = ");
	huajian(a1 * b2 + a2 * b1, b1 * b2);printf("\n");
	huajian(a1, b1);printf(" - ");huajian(a2, b2);printf(" = ");
	huajian(a1 * b2 - a2 * b1, b1 * b2);printf("\n");
	huajian(a1, b1);printf(" * ");huajian(a2, b2);printf(" = ");
	huajian(a1 * a2, b1 * b2);printf("\n");
	huajian(a1, b1);printf(" / ");huajian(a2, b2);printf(" = ");
	huajian(a1 * b2, b1 * a2);printf("\n");
	return 0;
}

傻傻的代码,删了可惜QAQ...

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;
struct node {
	long long int fenzi, fenmu;
	node() {
		fenzi = 0; fenmu = 0;
	}
};
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}
void huajian(long long int fenzi, long long int fenmu) {
	if (fenmu == 0) {
		printf("Inf"); return;
	}
	if (fenzi == 0) {
		printf("0"); return;
	}
	bool fuhao = 1;
	if ((fenzi < 0 && fenmu > 0) || (fenzi > 0 && fenmu < 0))fuhao = 0;
	if (fuhao == false)printf("(-");
	if (abs(fenzi) >= abs(fenmu)) {//假分数,先求出整数部分
		printf("%lld", abs(fenzi / fenmu));
		fenzi = fenzi % fenmu;
		if (fenzi != 0)printf(" ");
	}
	if (fenzi != 0) {
		long long k = gcd(abs(fenzi), abs(fenmu));
		fenzi = fenzi / k;fenmu = fenmu / k;
		printf("%lld/%lld", abs(fenzi), abs(fenmu));
	}
	if (fuhao == false)printf(")");
}
int main() {
	node op[2];
	for (int i = 0;i < 2;i++) {
		string stemp;
		int flag = true;//负数是0
		cin >> stemp;
		if (stemp[0] == '-') {
			flag = false;
			stemp.erase(stemp.begin());
		}
		int j = 0;
		while (stemp[j] != '/' && j < stemp.size()) {
			op[i].fenzi = op[i].fenzi * 10 + stemp[j] - '0';
			j++;
		}
		if (flag == false)op[i].fenzi = -op[i].fenzi;
		if (j == stemp.size())op[i].fenmu = 1;
		j++;
		while (j < stemp.size()) {
			op[i].fenmu = op[i].fenmu * 10 + stemp[j] - '0';
			j++;
		}
	}
	huajian(op[0].fenzi, op[0].fenmu);printf(" + ");huajian(op[1].fenzi, op[1].fenmu);printf(" = ");
	huajian(op[0].fenzi * op[1].fenmu + op[1].fenzi * op[0].fenmu, op[0].fenmu * op[1].fenmu);printf("\n");
	huajian(op[0].fenzi, op[0].fenmu);printf(" - ");huajian(op[1].fenzi, op[1].fenmu);printf(" = ");
	huajian(op[0].fenzi * op[1].fenmu - op[1].fenzi * op[0].fenmu, op[0].fenmu * op[1].fenmu);printf("\n");
	huajian(op[0].fenzi, op[0].fenmu);printf(" * ");huajian(op[1].fenzi, op[1].fenmu);printf(" = ");
	huajian(op[0].fenzi * op[1].fenzi, op[0].fenmu * op[1].fenmu);printf("\n");
	huajian(op[0].fenzi, op[0].fenmu);printf(" / ");huajian(op[1].fenzi, op[1].fenmu);printf(" = ");
	huajian(op[0].fenzi * op[1].fenmu, op[0].fenmu * op[1].fenzi);printf("\n");
	return 0;
}
posted @ 2020-07-23 13:08  \0.0/  阅读(221)  评论(0编辑  收藏  举报