日期:2014-05-20  浏览次数:20744 次

关于杨辉三角的一点问题
Java code

public class YangHuiTriangle {
    private int n;// 表示行

    public YangHuiTriangle(int n) {
        this.n = n;
    }

    public void printYangHuiTriangle() {
        System.out.println("杨辉三角(" + n + ")层:");
        System.out.println(1);
        for (int i = 1; i <= n - 1; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0)
                    System.out.print(1 + "\t");
                else {
                    System.out.print(calc(i, j) + "\t");
                }
            }
            System.out.println();
        }
    }
    
    //计算i行j列上的值,i,j从0开始
    public long calc(int i, int j) {
        double a = 1.0;//i行j列上的值
        for (int k = 1; k <= j; k++) {
            a *= (double)i / k;
            i--;
        }
        return (long)a;
    }

    public static void main(String[] args) {
        YangHuiTriangle y = new YangHuiTriangle(20);
        y.printYangHuiTriangle();
    }

}



写个杨辉三角玩玩,但发现结果中第12行和16行有点问题.如下:
12行:1 11 55 165 330 461 461 329 164 54 10 0
16行:1 15 105 454 1364 3002 5004 6434 6434 5004 3002 1364 454 104 14 0
望高人指点.谢谢.

------解决方案--------------------
为啥不直接用整数运算呢?
------解决方案--------------------
double在运算的时候会出现损失精度的情况
------解决方案--------------------
用浮点数来运算,自然存在误差,精度问题
杨辉三角完全没必要用浮点数啊
Java code

public class YangHuiTriangle {
    private int n;// 表示行

    public YangHuiTriangle(int n) {
        this.n = n;
    }

    public void printYangHuiTriangle() {
        System.out.println("杨辉三角(" + n + ")层:");
        for (int i = 0; i < n ; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(calc(i, j) + "\t");
            }
            System.out.println();
        }
    }


    //递归计算C(i,j) = 1 (k=0或者j=i);C(i,j) = C(i-1,j-1) + C(i-1,j)
    public long calc(int i, int j) {
        if (( j == 0 ) || (j == i)) return 1;
        return calc(i-1, j-1) + calc(i-1, j);
    }

    public static void main(String[] args) {
        YangHuiTriangle y = new YangHuiTriangle(20);
        y.printYangHuiTriangle();
    }

}

------解决方案--------------------
数字太大了,用BigInteger即可。
Java code
import java.math.BigInteger;

public class YangHuiTriangle {
    private int n;// 表示行

    public YangHuiTriangle(int n) {
        this.n = n;
    }

    public void printYangHuiTriangle() {
        System.out.println("杨辉三角(" + n + ")层:");
        System.out.println(1);
        for (int i = 1; i <= n - 1; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0)
                    System.out.print(1 + "\t");
                else {
                    System.out.print(calc(i, j) + "\t");
                }
            }
            System.out.println();
        }
    }

    //计算i行j列上的值,i,j从0开始
    public BigInteger calc(int i, int j) {
        BigInteger a = new BigInteger("1") ;//i行j列上的值
        for (int k = 1; k <= j; k++) {
            a = a.multiply(new BigInteger(String.valueOf(i))).divide(new BigInteger(String.valueOf(k)));
            i--;
        }
        return a;
    }

    public static void main(String[] args) {
        YangHuiTriangle y = new YangHuiTriangle(20);
        y.printYangHuiTriangle();
    }

}