很简单的题目,就是麻烦,考验耐心与细心
本题要求编写程序,计算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 a, b, c, d; //辗转相除法,用于计算两个正整数t1,t2的最大公约数 long long int gcd(long long int t1, long long int t2){ return t2 == 0 ? t1 : gcd(t2,t1%t2); } //int为32位,long long为64位,2^63 -1 //m是分子,n是分母 void func(long long int m, long long int n){ int flag1 = 0,flag2=0; if (n == 0){ cout << "Inf"; return; } if (m == 0){ cout << 0; return; } if (m < 0){ m = 0 - m; flag1 = 1; } if (n < 0){ n = 0 - n; flag2 = 1; } int flag = 0; if (flag1 == 1 && flag2 == 1){ flag = 0; } else if (flag1 == 1 || flag2 == 1){ flag = 1; } if (m == n){ if (flag1 == 1) cout << "(-1)"; else cout << "1"; } long long int x = m%n;//分数部分的分子 long long int y = m / n;//整数部分 if (x == 0){ if (flag == 0) cout << y; else cout << "(-" << y << ")"; return; } else{ long long int t1 = m - y*n;//分数部分的分子 long long int t2 = n;//分母 //求出分子与分母的最大公约数以得到最简分数部分 long long int t = gcd(t1, t2); t1 = t1 / t; t2 = t2 / t; if (flag == 1){ cout << "(-"; if (y != 0) cout << y << " " << t1 << "/" << t2; else cout << t1 << "/" << t2; cout << ")"; } else{ if (y != 0) cout << y << " " << t1 << "/" << t2; else cout << t1 << "/" << t2; } } } //加法 void add(){ long long int m, n; m = a*d + b*c; n = b*d; func(a, b); cout << " + "; func(c, d); cout << " = "; func(m, n); cout << endl; } //减法 void min(){ long long int m, n; m = a*d - b*c; n = b*d; func(a, b); cout << " - "; func(c, d); cout << " = "; func(m, n); cout << endl; } void multi() { long long int m, n; m = a * c; n = b * d; func(a, b); cout << " * "; func(c, d); cout << " = "; func(m, n); cout << endl; } void div() { long long int m, n; m = a * d; n = b * c; func(a, b); cout << " / "; func(c, d); cout << " = "; func(m, n); cout << endl; } void PAT1034(){ //还是觉得C语言的scanf格式输入比cin好用一些 scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d); add(); min(); multi(); div(); return; }