-1

我需要做的是取 2 个随机变量作为分数,从 1 到 1000,并检查它们是否已经简化了。我这样做了 1,000 次,并跟踪它是否是简化的。

这是主要课程

import java.util.*;

public class ratio1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int nonReducedCount = 0; //counts how many non reduced ratios there are
        for(int i =1; i<=1000; i++){

            Random rand = new Random();
            int n = rand.nextInt(1000)+1;  //random int creation
            int m = rand.nextInt(1000)+1;
            Ratio ratio = new Ratio(n,m);
            if (ratio.getReduceCount() != 0 ){ // if the ratio was not already fully reduced
                nonReducedCount++; // increase the count of non reduced ratios
            }   
        }

        int reducedCount = 1000 - nonReducedCount; //number of times the ratio was reduced already
        double reducedRatio = reducedCount / nonReducedCount; //the ratio for reduced and not reduced
        reducedRatio *= 6;
        reducedRatio = Math.sqrt(reducedRatio);
        System.out.println("pi is " + reducedRatio);
    }
}

这是我不确定的课程。我想要的只是确定分数是否已经是最简单的形式。当我目前尝试运行它时,它给了我一个错误;“Ratio.gcd (Ratio.java:67) 处 Ratio.gcd (Ratio.java:66) 处的线程“主”java.lang.StackOverflowError 异常”

public class Ratio{
    protected int numerator; // numerator of ratio
    protected int denominator; //denominator of ratio
    public int reduceCount = 0; //counts how many times the reducer goes

    public Ratio(int top, int bottom)
    //pre: bottom !=0
    //post: constructs a ratio equivalent to top::bottom
    {
        numerator = top;
        denominator = bottom;
        reduce();
    }

    public int getNumerator()
    //post: return the numerator of the fraction
    {
        return numerator;
    }

    public int getDenominator()
    //post: return the denominator of the fraction
    {
        return denominator;
    }

    public double getValue()
    //post: return the double equivalent of the ratio 
    {
        return (double)numerator/(double)denominator;

    }
    public int getReduceCount()
    //post: returns the reduceCount
    {
        return reduceCount;
    }

    public Ratio add(Ratio other)
    //pre: other is nonnull
    //post: return new fraction--the sum of this and other
    {
        return new Ratio(this.numerator*other.denominator+this.denominator*other.numerator,this.denominator*other.denominator);

    }

    protected void reduce()
    //post: numerator and denominator are set so that the greatest common divisor of the numerator and demoninator is 1
    {
        int divisor = gcd(numerator, denominator);
        if(denominator < 0) divisor = -divisor;
        numerator /= divisor;
        denominator /= divisor;
        reduceCount++;
    }

    protected static int gcd(int a, int b)
    //post: computes the greatest integer value that divides a and b
    {
        if (a<0) return gcd(-a,b);
        if (a==0){
            if(b==0) return 1;
            else return b;

        }
        if (b>a) return gcd(b,a);
        return gcd(b%a,a);
    }
    public String toString()
    //post:returns a string that represents this fraction.
    {
        return getNumerator()+"/"+getDenominator();

    }   
}

以下是 Ratio 类中的错误行;

if (b>a) return gcd(b,a);
        return gcd(b%a,a);
4

1 回答 1

1

如果 GCD 大于 1,则分数是可约的。您可以使用 Ratio 中给出的静态方法计算 GCD,因此您可以改用:

...
int n = rand.nextInt(1000)+1;
int m = rand.nextInt(1000)+1;
if(Ratio.gcd(n,m) == 1) {
    nonReducedCount++;
}

这使您免于实例化新的 Ratio 实例。

如果该方法不适合您,您可以随时使用自己的 GCD 计算器。这个也是递归的,类似于 Ratio 中的那个:

public static int gcd(int a, int b) { return b==0 ? a : gcd(b,a%b); }

如果 StackOverflowError 仍然是一个问题,你可以用谷歌搜索非递归方法。

于 2013-08-31T15:55:16.520 回答