一、 题目描述
本题要求编写程序,计算 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
结尾无空行
二、代码
其实我本来是按照数字的情况去分类的,后来发现这么写实在是需要区分太多种情况了,实际上也肯定即不能使用、考试时写也会浪费太多时间,所以参考了柳婼女神的思路,学到了很多,果然还是要多动脑,强行模拟情况还是不可行的。
这是我没有写完的
#define CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//1034
//数字输出
void Print_NegativeNumber(int k, int m, int n)
{
if (k > 0) k = -k;
if (m > 0) m = -m;
cout << "(";
if (k == 0)
cout << m<< " / "<< n << ") ";
else
cout << k<<" "<< -m<< " / "<< n <<") ";
}
void Print_PositiveNumber(int k, int m, int n)
{
if (k < 0) k = -k;
if (m < 0) m = -m;
if (k == 0)
cout << m << " / " << n ;
else
cout << k << " " << m << " / " << n ;
}
//数字预处理
int Preprocess_Simplicity(int m, int n)
{
int k = 0;
k = m / n;
return k;
}
int Preprocess_gcd(int n1, int n2)
{
return n2 == 0 ? n1 : Preprocess_gcd(n2, n1 % n2);
}
//数字计算
void Compute_IncludeZero(int a1, int b1, int a2, int b2)
{
if (a1 == 0)
{
//a2有三种状态
if (a2 == 0)
{
cout << "0 + 0 = 0" << endl;
cout << "0 - 0 = 0" << endl;
cout << "0 * 0 = 0" << endl;
cout << "0 / 0 = Inf" << endl;
}
else//a2 != 0
{
if (a2 > 0)
{
int k = Preprocess_Simplicity(a2, b2);a2 = a2 % b2;
cout << "0 + "; Print_PositiveNumber(k, a2, b2); cout << " = "; Print_PositiveNumber(k, a2, b2); cout << endl;
cout << "0 - "; Print_PositiveNumber(k, a2, b2); cout << " = "; Print_NegativeNumber(k, a2, b2); cout << endl;
cout << "0 * "; Print_PositiveNumber(k, a2, b2); cout << " = 0" << endl;
cout << "0 / "; Print_PositiveNumber(k, a2, b2); cout << " = 0" << endl;
}
else//a2 < 0
{
int k = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
cout << "0 + "; Print_NegativeNumber(k, a2, b2); cout << " = "; Print_NegativeNumber(k, a2, b2); cout<< endl;
cout << "0 - "; Print_NegativeNumber(k, a2, b2); cout << " = "; Print_PositiveNumber(k, a2, b2); cout << endl;
cout << "0 * "; Print_NegativeNumber(k, a2, b2); cout << " = 0"<< endl;
cout << "0 / "; Print_NegativeNumber(k, a2, b2); cout << " = 0"<< endl;
}
}
}
else//a1!=0&&a2==0
{
if (a1 > 0)
{
int k = Preprocess_Simplicity(a1, b1); a1 = a1 % b1;
Print_PositiveNumber(k, a1, b1); cout << " + 0 = "; Print_PositiveNumber(k, a1, b1); cout << endl;
Print_PositiveNumber(k, a1, b1); cout << " - 0 = "; Print_PositiveNumber(k, a1, b1); cout << endl;
Print_PositiveNumber(k, a1, b1); cout << " * 0 = 0" << endl;
Print_PositiveNumber(k, a1, b1); cout << " / 0 = Inf" << endl;
}
else//a1 < 0
{
int k = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
Print_NegativeNumber(k, a1, b1); cout << " + 0 = "; Print_NegativeNumber(k, a1, b1); cout << endl;
Print_NegativeNumber(k, a1, b1); cout << " - 0 = "; Print_NegativeNumber(k, a1, b1); cout << endl;
Print_NegativeNumber(k, a1, b1); cout << " * 0 = 0" << endl;
Print_NegativeNumber(k, a1, b1); cout << " / 0 = Inf" << endl;
}
}
}
void Compute_ExceptZero(int a1, int b1, int a2, int b2)
{
if (a1 > 0 )
{
int k1 = Preprocess_Simplicity(a1, b1); a1 = a1 % b1;
int k_temp, m_temp, n_temp,temp;
if (a2 > 0)//a1>0&&a2>0
{
int k2 = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
//add
n_temp = b1 * b2 / Preprocess_gcd(b1, b2);
m_temp = (a1 * b2 / Preprocess_gcd(b1, b2) + a2 * b1 / Preprocess_gcd(b1, b2)) % n_temp;
k_temp = k1 + k2 + (a1 * b2 / Preprocess_gcd(b1, b2) + a2 * b1 / Preprocess_gcd(b1, b2)) / n_temp;
Print_PositiveNumber(k1, a1, b1); cout << " + "; Print_PositiveNumber(k2, a2, b2); cout << " = "; Print_PositiveNumber(k_temp, m_temp, n_temp );
//sub
m_temp = (a1 * b2 / Preprocess_gcd(b1, b2) - a2 * b1 / Preprocess_gcd(b1, b2)) % n_temp;
k_temp = k1 - k2 + (a1 * b2 / Preprocess_gcd(b1, b2) - a2 * b1 / Preprocess_gcd(b1, b2)) / n_temp;
if(k_temp>0||(k_temp==0&&m_temp>0))
//mul
//div
}
else//a1>0&&a2<0
{
int k2 = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
}
}
else//a1<0
{
int k1 = Preprocess_Simplicity(a1, b1); a1 = a1 % b1;
if (a2 > 0)//a1<0&&a2>0
{
int k2 = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
}
else//a1<0&&a2<0
{
int k2 = Preprocess_Simplicity(a2, b2); a2 = a2 % b2;
}
}
}
int main()
{
int a1, b1, a2, b2;
//接收数据
scanf("%d/%d %d/%d", &a1, &b1, &a2, &b2);
//判断是否有含0数据
if (a1 == 0 || a2 == 0)
Compute_IncludeZero(a1, b1, a2, b2);
else
Compute_ExceptZero(a1, b1, a2, b2);
return 0;
}
这是上文提到的柳婼前辈的,
#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;
}
题目合集
如果你对其他题目也感兴趣,可以点这里~