[PAT] A1088 Rational Arithmetic
阿新 • • 發佈:2020-07-23
題目大意
給出兩個分數,求加減乘除的結果。
思路
求最大公約數要用輾轉相除法,否則會超時。
判斷符號不能兩數相乘,因為乘積可能超過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; }