PAT (Basic Level) Practice 1034 有理数四则运算 (20分) (辗转相除求最大公因数、)
1.题目
本题要求编写程序,计算 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
2.题目分析
输入开始是用了字符串截取数字再转,后来直接scanf也是可以接受多种变化而接受数字的(菜……💢)
3.代码
我的代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;
long long int a1, b1, a2, b2;
long long int add1, add2, sub1, sub2, mul1, mul2;
int gcd(long long int a, long long int b)
{
return b == 0 ? a : gcd(b, a%b);
}
void output(long long int tempint, long long int tempa, long long int tempb)
{
if (tempint == 0)
{
if (tempa > 0)printf("%lld/%lld", tempa, tempb);
else if (tempa == 0)printf("0");
else printf("(%lld/%lld)", tempa, tempb);
}
else if (tempint > 0)
{
if (tempa > 0)printf("%lld %lld/%lld", tempint, tempa, tempb);
else if (tempa == 0)printf("%lld", tempint);
}
else
{
if (tempa > 0)printf("(%lld %lld/%lld)", tempint, tempa, tempb);
else if (tempa == 0)printf("(%lld)", tempint);
}
}
void process(long long int tempa,long long int tempb)
{
long long int outint=0, outa=tempa, outb=tempb;
long long int c = gcd(tempa, tempb);
if (c != 1&&c!=-1) { outa = tempa / abs(c); outb = tempb / abs(c); }//约分
if (abs(outa) >= abs(outb)) { outint = outa / outb; outa = abs(outa) - abs(outint)*abs(outb); outb = abs(outb); }//有理化
else
{
if (outa < 0 && outb < 0) { outa = abs(outa); outb = abs(outb); }
else if (outa > 0 && outb < 0) { outa = -outa; outb = abs(outb);}
}
output(outint, outa, outb);
}
int main()
{
//string temp;
//getline(cin, temp);
//long long int ispositive1 = 1;
//long long int ispositive2 = 1;
//if (temp[0] == '-') { ispositive1 = -1; temp.erase(0, 1); }//判断第一个正负
//if (temp.find('-') != string::npos) {ispositive2 = -1; temp.erase(temp.find('-'), 1);}//判断第二个正负
//stringstream ss;
//string tempnumber = temp.substr(0, temp.find_first_of('/'));ss << tempnumber;ss >> a1;a1 =a1*ispositive1;
//stringstream ss2;
//tempnumber = temp.substr(temp.find_first_of('/')+1, temp.find_first_of(' '));ss2 << tempnumber;ss2 >> b1;
//stringstream ss3;
//tempnumber = temp.substr(temp.find_first_of(' '), temp.find_last_of('/'));ss3 << tempnumber;ss3 >> a2;a2 = a2*ispositive2;
//stringstream ss4;
//tempnumber = temp.substr(temp.find_last_of('/')+1, temp.length()-1);ss4 << tempnumber;ss4 >> b2;
scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
long long int d = b1*b2/gcd(b1, b2);//分母的最小公倍数
long long int aa1 = d / b1*a1;
long long int aa2 = d / b2*a2;
//加法
add1 = aa1+aa2;
add2 = d;
process(a1,b1);
printf(" + ");
process( a2, b2);
printf(" = ");
process( add1, add2);
printf("\n");
//减法
sub1 = aa1-aa2;
sub2 = d;
process(a1, b1);
printf(" - ");
process(a2, b2);
printf(" = ");
process( sub1, sub2);
printf("\n");
//乘法
mul1 = a1*a2;
mul2 = b1*b2;
process( a1, b1);
printf(" * ");
process( a2, b2);
printf(" = ");
process( mul1, mul2);
printf("\n");
//除法
process(a1, b1);
printf(" / ");
process(a2, b2);
printf(" = ");
mul1 = a1*b2;
mul2 = b1*a2;
if (mul2 == 0)
{
printf("Inf\n"); return 0;
}
process(mul1, mul2);
printf("\n");
}
大神代码:(https://blog.csdn.net/liuchuo/article/details/51985875)
#include <iostream>
#include <cmath>
using namespace std;
long long a, b, c, d;
long long gcd(long long t1, long long t2) {
return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(long long m, long long n) {
if (m * n == 0) {
printf("%s", n == 0 ? "Inf" : "0");
return ;
}
bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
m = abs(m); n = abs(n);
long long x = m / n;
printf("%s", flag ? "(-" : "");
if (x != 0) printf("%lld", x);
if (m % n == 0) {
if(flag) printf(")");
return ;
}
if (x != 0) printf(" ");
m = m - x * n;
long long t = gcd(m, n);
m = m / t; n = n / t;
printf("%lld/%lld%s", m, n, flag ? ")" : "");
}
int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
return 0;
}