当前位置: 代码迷 >> Java相关 >> 惭愧啊 被这个小题目给难道了 帮忙看看
  详细解决方案

惭愧啊 被这个小题目给难道了 帮忙看看

热度:138   发布时间:2010-07-29 19:25:36.0
惭愧啊 被这个小题目给难道了 帮忙看看
内容:


1/x2+1/y2=1/z2 其中正整数xyz成为一组倒立的勾股数!注意,是正整数哦!

你的任务是输出60以内的倒立勾股数,按x的的增序输出(每行一个)。


public class Main
{
  public static void main(String[] args)
    {
            //  Scanner a = new Scanner(System.in);
             //  String b=a.next();
     double a,b,c;
      for( int x=1;x<61;x++){
         // a=1/(x*x);
           a=(double)(1.0/x*x);
              for(int y=1;y<61;y++){
               b=(double)(1.0/(y*y));

                     for(int z=1;z<61;z++){
                      c=(double)(1.0/(z*z));

                          if(a+b==c){
                          System.out.println(x+" "+y+" "+z);

               }
            }
         }
       }
    }
}
----------------解决方案--------------------------------------------------------
.........这样做肯定不行的,用通分啊。
----------------解决方案--------------------------------------------------------
好的  我去试试
----------------解决方案--------------------------------------------------------
public class Shu
{
  public static void main(String[] args)
    {
            //  Scanner a = new Scanner(System.in);
             //  String b=a.next();
     double a,b,c;
      for( double x=1;x<61;x++){
         // a=1/(x*x);
           a=(1.0/x*x);
              for(double y=1;y<61;y++){
               b=(1.0/(y*y));

                     for(double z=1;z<61;z++){
                      c=(1.0/(z*z));

                          if(a+b-c<0.0001&&a+b-c>-0.0001){//误差范围内都以为相等
                          System.out.println(x+" "+y+" "+z);

               }
            }
         }
       }
    }
}

[ 本帖最后由 aufish 于 2010-7-29 20:44 编辑 ]
----------------解决方案--------------------------------------------------------
double float 都会存在  0.99999999999999!=1的 所以不要用==
----------------解决方案--------------------------------------------------------
通分是王道  见识了  以后遇见分数的就先考虑的  唉  脑子不好使了 放小学都会的
----------------解决方案--------------------------------------------------------
一个分数类
程序代码:
import java.math.BigInteger;

public class Fraction {

    BigInteger numerator, denominator, gcd;

    void set(String n, String d) {
        numerator = new BigInteger(n);
        denominator = new BigInteger(d);
    }

    void simplify() {
        gcd = numerator.gcd(denominator);
        numerator = numerator.divide(gcd);
        denominator = denominator.divide(gcd);
        if (denominator.compareTo(BigInteger.ZERO) < 0) {
            numerator = numerator.negate();
            denominator = denominator.negate();
        }
    }

    int compareTo(Fraction y) {
        Fraction z = this.subtract(y);
        return z.numerator.compareTo(BigInteger.ZERO);
    }

    Fraction abs() {
        Fraction z = new Fraction();
        if (numerator.compareTo(BigInteger.ZERO) < 0) {
            z.numerator = numerator.negate();
        } else {
            z.numerator = numerator;
        }
        if (denominator.compareTo(BigInteger.ZERO) < 0) {
            z.denominator = denominator.negate();
        } else {
            z.denominator = denominator;
        }
        return z;
    }

    Fraction negate() {
        Fraction z = this;
        z.numerator = z.numerator.negate();
        return z;
    }

    boolean zero() {
        return numerator.compareTo(BigInteger.ZERO) == 0;
    }

    Fraction add(Fraction y) {
        Fraction z = new Fraction();
        gcd = denominator.gcd(y.denominator);
        z.denominator = denominator.multiply(y.denominator).divide(gcd);
        z.numerator = numerator.multiply(z.denominator.divide(denominator)).add(y.numerator.multiply(z.denominator.divide(y.denominator)));
        z.simplify();
        return z;
    }

    Fraction subtract(Fraction y) {
        Fraction z = new Fraction();
        gcd = denominator.gcd(y.denominator);
        z.denominator = denominator.multiply(y.denominator).divide(gcd);
        z.numerator = numerator.multiply(z.denominator.divide(denominator)).subtract(y.numerator.multiply(z.denominator.divide(y.denominator)));
        z.simplify();
        return z;
    }

    Fraction multiply(Fraction y) {
        Fraction z = new Fraction();
        z.numerator = numerator.multiply(y.numerator);
        z.denominator = denominator.multiply(y.denominator);
        z.simplify();
        return z;
    }

    Fraction divide(Fraction y) {
        Fraction z = new Fraction();
        z.numerator = numerator.multiply(y.denominator);
        z.denominator = denominator.multiply(y.numerator);
        z.simplify();
        return z;
    }

    void print() {
        if (denominator.compareTo(BigInteger.ONE) != 0) {
            System.out.printf("%s/%s", numerator.toString(), denominator.toString());
        } else {
            System.out.printf("%s", numerator.toString());
        }
        System.out.println();
    }




----------------解决方案--------------------------------------------------------
用通分看下吧。。。

----------------解决方案--------------------------------------------------------
  相关解决方案