1

这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。

import java.math.BigInteger;

public class Rational {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;


/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}

/** Construct a rational with specified numerator and denominator */
public Rational(BigInteger numerator, BigInteger denominator) {
   BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator, 
   denominator)));
   BigInteger r1=new 
   BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
     this.numerator = (r1.multiply(numerator)).divide(gcd);
     this.denominator = (denominator.abs()).divide(gcd);
    }

 /** Find GCD of two numbers */
 private static long gcd(BigInteger n, BigInteger d) {
 BigInteger n1 = n.abs();
 BigInteger n2 = d.abs();
 int gcd = 1;

 for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 && 
 (new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
 if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) && 
 n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO)) 
 gcd = k;
 }

 return gcd;
 }

 /** Return numerator */
 public BigInteger getNumerator() {
  return numerator;
 }

 /** Return denominator */
 public BigInteger getDenominator() {
  return denominator;
 }

 /** Add a rational number to this rational */
 public Rational add(Rational secondRational) {
  BigInteger n = 
numerator.multiply(secondRational.getDenominator())
.add(denominator.multiply(sec
ondRational.getNumerator()));
  BigInteger d = denominator.multiply(secondRational.getDenominator());
  return new Rational(n, d);
 }

 /** Subtract a rational number from this rational */
 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);
 }

 /** Multiply a rational number to this rational */
 public Rational multiply(Rational secondRational) {
     BigInteger n = numerator.multiply(secondRational.getNumerator());
     BigInteger d = denominator.multiply(secondRational.getDenominator());
  return new Rational(n, d);
 }

 /** Divide a rational number from this rational */
 public Rational divide(Rational secondRational) {
     BigInteger n = numerator.multiply(secondRational.getDenominator());
     BigInteger d = denominator.multiply(secondRational.numerator);
  return new Rational(n, d);
 }

/** Compute the square of this rational number*/
 public Rational square() {
     BigInteger n = numerator.multiply(numerator);
     BigInteger d = denominator.multiply(denominator);
  return new Rational(n, d);
 }

/** toString */
 public String toString() {
      return numerator + "/" + denominator;
 }
}

这是 testRational 类 import java.math.BigInteger;

public class TestRational {
   public static void main(String[]args){
    int y = 1;

    BigInteger i=new BigInteger(String.valueOf(1));
    BigInteger a=new BigInteger(String.valueOf(2));
    BigInteger b=new BigInteger(String.valueOf(3));
    BigInteger c=new BigInteger(String.valueOf(5));



    Rational sum = new  Rational(BigInteger.ZERO,a);
    Rational r0 = new Rational(b,b.add(i));
    Rational r2 = new  Rational(a,c);
    Rational r3 = new  Rational(a,c);


    Rational s1 = r3.multiply(r2);
    Rational s2 = r3.square();
    Rational s3 = r2.divide(r3);

    Rational r1 = new  Rational(i,a);
    do{

        sum = sum.add(r0);
        b = b.add(a);
        y++;




    }while(y<49);
        System.out.println(sum.multiply(sum));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(r0);
}
}

这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。

4

2 回答 2

0

目前尚不清楚您的数字序列是什么,但我将采用以下假设:

如果您的目标是简单地返回(1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2. 那么我会建议以下几点:

//This method will return the value of (1/2 + 3/4 + 5/6 + ... + 97/98 + 99/100) ^ 2
public int calc(){
    double denominator = 2;
    double numerator = denominator - 1; //in your sequence, numerator is always 1 less than denominator
    double sum = 0;

    while(denominator <= 100){
         sum = sum + (numerator / denominator); //shorthand sum += (numerator / denominator);
         denominator = denominator + 2; //shorthand denominator += 2;
         numerator = denominator - 1;
    }

    return sum * sum; //this is equivalent to sum ^ 2
}
于 2017-09-27T14:35:25.623 回答
0

让我们把它写得不那么混乱,没有所有不必要的东西和令人困惑的循环。和的定义是 (1/2 + 3/4 ... 99/100) 所以让我们从创建和中的所有分数开始:

for (int i = 1; i <= 99; i += 2) {
    BigRational t = new BigRational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
}

它们必须相加,因此必须在循环外声明一个变量,以将所有这些分数相加:

Rational sum = new Rational();
for (int i = 1; i <= 99; i += 2) {
    Rational t = new Rational(BigInteger.valueOf(i), BigInteger.valueOf(i + 1));
    sum = sum.add(t);
}

然后平方,你就有了答案。我得到:

87593039510089573189394173247956745677798336081
-----------------------------------------------
   38416307357189261992010230523038591203840000

我无法验证,但它看起来很合理。预期的答案是“略小于 50 2 ”(因为 50 项的平方接近 1,如果可以称为 0.5 的话),这已经足够接近了。

顺便说一句,停止String.valueOfRational. 只需使用数字即可。并且BigInteger已经实现gcd,您不必编写自己的(效率较低的)版本。我不得不更换这个,否则它花了太长时间。

于 2017-09-27T14:35:50.100 回答