我使用BigInteger创建了Rational类,但没有显示任何内容 import java.math.*; 导入java.util.Scanner; 公共班机{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); int T=sc.nextInt(); for(int t=0;t0)?新的BigInteger(“1”):新的BigInteger(“-1”)) .乘(分子除(gcd)); this.denominor=denominor.abs().除法(gcd); } //求两个大整数的GCD 私有静态BigInteger gcd(BigInteger n,BigInteger d){ BigInteger n1=n.abs(); 大整数n2=d.abs(); BigInteger gcd=BigInteger.1; for(biginger k=biginger.ONE;k.compareTo(n1)
我使用BigInteger创建了Rational类,但没有显示任何内容 import java.math.*; 导入java.util.Scanner; 公共班机{ 公共静态void main(字符串[]args){ 扫描仪sc=新的扫描仪(System.in); int T=sc.nextInt(); for(int t=0;t0)?新的BigInteger(“1”):新的BigInteger(“-1”)) .乘(分子除(gcd)); this.denominor=denominor.abs().除法(gcd); } //求两个大整数的GCD 私有静态BigInteger gcd(BigInteger n,BigInteger d){ BigInteger n1=n.abs(); 大整数n2=d.abs(); BigInteger gcd=BigInteger.1; for(biginger k=biginger.ONE;k.compareTo(n1),java,Java,biginger是不可变的。您不能执行k.add(biginger.ONE)并期望k更改。您必须执行k=k.add(biginger.ONE) 在构造过程中调用的gcd方法中可以实现这一点,因此第一次创建Rationalgcd时会进入一个无限循环。biginger是不可变的。你不能做k.add(biginger.ONE)而期望k改变。你必须做k=k.add(BigInteger.ONE) 您可以在构建过程中调用的gcd方法中执行此操作,因此第一次创建Rationalgcd将进入无限循环。@gir
biginger
是不可变的。您不能执行k.add(biginger.ONE)
并期望k
更改。您必须执行k=k.add(biginger.ONE)
在构造过程中调用的
gcd
方法中可以实现这一点,因此第一次创建Rational
gcd
时会进入一个无限循环。biginger
是不可变的。你不能做k.add(biginger.ONE)
而期望k
改变。你必须做k=k.add(BigInteger.ONE)
您可以在构建过程中调用的
gcd
方法中执行此操作,因此第一次创建Rational
gcd
将进入无限循环。@girlcrush请单击此答案旁边的复选标记图标将此问题标记为已解决。谢谢。@girlcrush请将此问题标记为cli已解决点击此答案旁边的复选标记图标。谢谢。谢谢。哦,现在程序工作,但时间限制超过了…;(谢谢。哦,现在程序工作,但时间限制超过了…)(
import java.math.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
Rational r1 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));
Rational r2 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
}
}
}
class Rational extends Number implements Comparable<Rational> {
private static final long serialVersionUID = 1L;
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
// set numerator and denominator
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO) > 0) ? new BigInteger("1") : new BigInteger("-1"))
.multiply(numerator.divide(gcd));
this.denominator = denominator.abs().divide(gcd);
}
// Find GCD of two BigIntegers
private static BigInteger gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
BigInteger gcd = BigInteger.ONE;
for (BigInteger k = BigInteger.ONE; k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0; k.add(BigInteger.ONE)) {
if (n1.mod(k).equals(BigInteger.ZERO) && n2.mod(k).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
public Rational add(Rational secondRational) {
BigInteger n = (numerator.multiply(secondRational.getDenominator()))
.add(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
BigInteger n = (numerator.multiply(secondRational.getDenominator()))
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
@Override
public String toString() {
if (denominator.compareTo(BigInteger.ONE) == 0)
return numerator + "";
else
return numerator + "/" + denominator;
}
@Override
public boolean equals(Object other) {
if ((this.subtract((Rational) (other))).getNumerator().equals(BigInteger.ZERO))
return true;
else
return false;
}
@Override
public int intValue() {
return (int) doubleValue();
}
@Override
public float floatValue() {
return (float) doubleValue();
}
@Override
public double doubleValue() {
double x = this.getNumerator().doubleValue();
double y = this.getDenominator().doubleValue();
return x / y;
}
@Override
public long longValue() {
return (long) doubleValue();
}
@Override
public int compareTo(Rational o) {
if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) > 0)
return 1;
else if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) < 0)
return -1;
else
return 0;
}
}