Rational Arithmetic (20)__牛客网 (nowcoder.com)
输入描述:
每个输入文件只包含一个测试用例,测试用例会给出一行数据,格式为“a1/b1 a2/b2”分子分母的范围都在长整型的范围内,如果数字为负,则符号只会出现在分子的前面。分母一定是非零数。
输出描述:
针对每个测试用例,都输出四行,分别是这两个有理数的和、差、积和商,格式为“数1 操作符 数2 = 结果”。注意,所有的有理数都将遵循一个简单形式“k a/b”,其中k是整数部分,a/b是最简分数形式,如果该数为负数,则必须用括号包起来。如果除法中的除数为0,则输出“Inf”。结果中所有的整数都在long int的范围内。
思路分析
要计算和、差、积和商
a1/b1 + a2/b2 = (a1*b2 + a2*b1) / (b1 * b2);
a1/b1 - a2/b2 = (a1*b2 - a2*b1) / (b1*b2);
a1/b1 * a2/b2 = (a1*a2) / (b1*b2);
(a1/b1) / (a2/b2) =(a1/b1) * (b2 / a2) = (a1 * b2) / (a2 * b1);
还要保证a/b是最简分数形式
分子和分母都除最大公约数
使用 辗转相除法,求最大公约数
遵循一个简单形式“k a/b”
负数,则必须用括号包起来
除数为0,则输出“Inf”
代码实现
import java.util.*;
class RationalNum {
//分子
private long numerator;
//分母
private long denominator;
//整数部分
private long integer;
//判断符号
private boolean isNegative = false;
//判断分母为零
private boolean isZero = false;
//参与运算的分子
private long totalNumerator;
//构造方法 对这些值初始化
public RationalNum(long n, long d) {
if(d == 0) {
isZero = true;
return;
}
if(n < 0) {
isNegative = true;
}
//经过除法计算可能将分母变为负数
if(d < 0) {
isNegative = !isNegative;
}
//如果输入是假分数转换为真分数
integer = n / d;
numerator = n - integer * d;
denominator = Math.abs(d);
//化简 分子分母同时除以最大公约数
if(numerator > 1 || numerator < -1) {
long gcd = isGcd(Math.abs(numerator), denominator);
if(gcd > 0) {
numerator /= gcd;
denominator /= gcd;
}
}
//进行计算的分子,一定是简化后
totalNumerator = integer * denominator + numerator;
}
//辗转相除法,求最大公约数
private long isGcd(long a, long b) {
if(b == 0) {
return a;
}
return isGcd(b,a % b);
}
//根据字符串得到分子
public static long parseNumerator(String s) {
return Long.parseLong(s.substring(0, s.indexOf("/")));
}
//根据字符串得到分母
public static long paresDenominator(String s) {
return Long.parseLong(s.substring(s.indexOf('/')+1));
}
//加法操作
public RationalNum add(RationalNum r) {
long n = totalNumerator * r.denominator + denominator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n, d);
}
//减法操作
public RationalNum sub(RationalNum r) {
long n = totalNumerator * r.denominator - denominator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n, d);
}
//乘法操作
public RationalNum mul(RationalNum r) {
long n = totalNumerator * r.totalNumerator;
long d = denominator * r.denominator;
return new RationalNum(n , d);
}
//除法操作
public RationalNum div(RationalNum r) {
long n = totalNumerator * r.denominator;
long d = denominator * r.totalNumerator;
return new RationalNum(n , d);
}
//对于输出进行重写
@Override
public String toString() {
StringBuffer s = new StringBuffer();
if(isZero) {
s.append("Inf");
return new String(s);
}
if(integer == 0 && numerator == 0) {
s.append("0");
return new String(s);
}
if(isNegative) {
s.append("(-");
}
//有整数,注意与分子空格
if(integer != 0) {
s.append(Math.abs(integer));
if(numerator != 0) {
s.append(' ');
}
}
//可能没有整数部分
if(numerator != 0) {
s.append(Math.abs(numerator));
s.append('/');
s.append(denominator);
}
if(isNegative) {
s.append(')');
}
return new String(s);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
//接收一个分数,next遇到换行空格等空白符停止,且不包含上述空白符
String s = in.next();
RationalNum r1 = new RationalNum(RationalNum.parseNumerator(s),RationalNum.paresDenominator(s));
//在接收第二个分数
s = in.next();
RationalNum r2 = new RationalNum(RationalNum.parseNumerator(s),RationalNum.paresDenominator(s));
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.sub(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.mul(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.div(r2));
}
}
}
结果显示
标签:编程,有理数,运算,denominator,long,b1,b2,return,RationalNum From: https://blog.51cto.com/u_16166203/7152316