Algorithms 4th - 1.1 Basic Programming Model - CREATIVE PROBLEMS

  欢迎交流

  1.1.26

public class TestApp {

    public static void main(String[] args) {



        int a = StdIn.readInt();

        int b = StdIn.readInt();

        int c = StdIn.readInt();

        int t;



        if( a > b) {

            t = a;

            a = b;

            b = t;

        }



        if( a > c) {

            t = a;

            a = c;

            c = t;

        }



        if( b > c) {

            t = b;

            b = c;

            c = t;

        }



        StdOut.println(a + "->" + b + "->" + c);

    }

}

   1.1.27

public class Binomial {

    /**

     * 递归方式的二项分布

     * @param N 总次数

     * @param k 出现次数

     * @param p 每次出现概率

     * @return

     */

    public static double binomial1(int N, int k, double p) {

        if(N == 0 && k == 0)

            return 1.0;

        if(N < 0 || k < 0)

            return 0.0;

        return (1.0 - p) * binomial1(N - 1, k, p) + p * binomial1(N - 1, k - 1, p);

    }



    public static double binomial2(int N, int k, double p) {

        double[][] b = new double[N + 1][N + 1];

        // base

        for(int i = 0; i <= N; i++) {

            b[i][0] = Math.pow(1.0 - p, i);

        }

        b[0][0] = 1.0;

        // recursive

        for(int i = 1; i <= N; i++) {

            for(int j = 1; j <=k; j++) {

                b[i][j] =  p * b[i - 1][j - 1] + (1.0 - p) * b[i - 1][j];

            }

        }

        return b[N][k];

    }



    public static void main(String[] args) {

        int N = 100;

        int k = 50;

        double p = 0.76;

        StdOut.println(binomial1(N, k, p));

        StdOut.println(binomial2(N, k, p));

    }

}

  1.1.28

  想不出与BinarySearch有关的去重方法。

  1.1.29

// returns the number of elements that are smaller than the key

    public static int rank(int key, int[] a) {

        int lo = 0;

        int hi = a.length - 1;

        while(lo <= hi) {

            int mid = (lo + hi) / 2;

            if(key < a[mid])

                hi = mid - 1;

            else if(key > a[mid])

                lo = mid + 1;

            else {

                while(a[mid - 1] == key) {

                    mid -= 1;

                }

                return mid;

            }

        }

        return -1;

    }



    // returns the number of elements equal to the key

    public static int count(int key, int[] a) {

        int lo = 0;

        int hi = a.length - 1;

        while(lo <= hi) {

            int mid = (lo + hi) / 2;

            if(key < a[mid])

                hi = mid - 1;

            else if(key > a[mid])

                lo = mid + 1;

            else {

                lo = mid;

                hi = mid;

                while(a[lo - 1] == key) {

                    lo -= 1;

                }

                while(a[hi + 1] == key) {

                    hi += 1;

                }

                return hi - lo + 1;

            }

        }

        return 0;

    }

 

你可能感兴趣的:(programming)