zoukankan      html  css  js  c++  java
  • 算法(第四版)练习 1.1.26 ~ 1.1.31

    1.1.26

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    public class MyTest {
        public static void main(String[] args) {
            int a = 4, b = 5, c = 3, t = 0;
            if (a > b) {
                t = a;
                a = b;
                b = t;            
            } // Make sure b is greater than a
            if (a > c) {
                t = a;
                a = c;
                c = t;            
            } // Make sure c is greater than a , At this time the value of a must be the youngest among the three
            if (b > c) {
                t = b;
                b = c;
                c = t;
            } // Compare the value of b and c , so that c get the maximum, then c > b > a
            out.println(a + " " + b + " " + c);
        }
    }
    /*
    output=
    3 4 5
    */

    1.1.27

    估算:2^100 + 2^50;

    该进版本:

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    public class MyTest {
        private static double[][] map = new double[150][150];
        
        static {
            for (int i = 0; i != 150; ++i)
                for (int j = 0; j != 150; ++j) {
                    map[i][j] = -1; // map[i][j] can't be initialized to zero! Otherwise you'll get a wrong answer.
                }
        }
        
        // Calculate the probability by recursion
        public static double binomial(int N, int k, double p) {
            if(N == 0 && k == 0) return 1.0;
            if(N < 0 || k < 0) return 0.0;
            if (map[N][k] == -1) {
                map[N][k] = (1.0 - p)*binomial(N-1, k, p) + p*binomial(N-1, k-1, p);
            }
            return map[N][k];
        }
        
        // Calculate the probability by formula
        public static double bino(int N, int k, double p) {
            double x = MyMath.f(N) / (MyMath.f(k) * MyMath.f(N-k));
            double y = MyMath.power(p, k) * MyMath.power(1-p, N-k);
            return x * y;
        }
        
        public static void main(String[] args) {
            out.println(bino(100, 50, 0.25));
            out.println(binomial(100, 50, 0.25));
        }
    }
    
    class MyMath {
        private static double[] a = new double[200];
        // Returns N!
        public static double f(int N) {
            if (N == 1) return 1;
            if (a[N] != 0.0) return a[N];
            a[N] = N * f(N-1);
            return a[N];
        }
        
        // Returns a^b
        public static double power(double a, double b) {
            double answer = 1;
            for (int i = 0; i != b; ++i) answer *= a;
            return answer;
        }
    }

    1.1.28

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    public class MyTest {
        // Remove duplicates.
        public static int[] RemoveDuplicates(int[] a) {
            List<Integer> temp = new ArrayList<>();
            temp.add(a[0]);
            for (int i = 1; i != a.length; ++i) {
                if (a[i] != a[i-1]) temp.add(a[i]);
            }
            
            int[] x = new int[temp.size()];
            for (int i = 0; i != x.length; ++i) {
                x[i] = temp.get(i);
            }
            return x;
        }
        
        public static void main(String[] args) {
            int[] a = {3, 3, 2, 1, 1, 11, 4, 22, 13, 15, 17, 10, 10};
            Arrays.sort(a);
            a = RemoveDuplicates(a);
            for (int x : a) {
                // Print the modified array
                out.print(x + " ");
            }
        }
    } /* output=1 2 3 4 10 11 13 15 17 22 */

    1.1.29

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    public class MyTest {
        // Returns the number of elements that are smaller than the key
        public static int rank(int key, int[] a) {
            int k = rank(key, a, 0, a.length - 1);
            if (k < 0) {
                return -1;
            } else {
                while (a[k-1] == a[k]) k--;
                return k;
            }
        }
        
        public static int rank(int key, int[] a, int lo, int hi) {
            if (lo > hi) return -1;
            int mid = lo + (hi - lo) / 2;
            if (key < a[mid]) return rank(key, a, lo, mid - 1);
            else if (key > a[mid]) return rank(key, a, mid + 1, hi);
            else return mid;
        }
        
        // Returns the number of elements equal to the key
        public static int count(int key, int[] a) {
            int k = 0;
            for (int x : a) {
                if (x == key) ++k;
            }
            return k;
        }
        
        public static void main(String[] args) {
            int[] a = {2, 3, 3, 3, 5, 9};
            Arrays.sort(a);
            out.println(rank(3, a) + " " + count(3, a));
        }
    }

    1.1.30

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    public class MyTest {
        // Returns the greatest common divisor of two numbers
        public static int gcd(int a, int b) {
            if (a%b == 0) return b;
            else return gcd(b, a%b);
        }
        
        // If a and b are co-prime, return true
        public static boolean coprime(int a, int b) {
            return (gcd(a, b) == 1 ? true : false);
        }
        
        public static void main(String[] args) {
            final int N = 200;
            boolean[][] a = new boolean[N][N];
            for (int i = 1; i != N; ++i)
                for (int j = 1; j != N; ++j) {
                    a[i][j] = coprime(i, j);
                    if (a[i][j]) out.println(i + " " + j);
                }
        }
    }

    1.1.31

    import static java.util.stream.IntStream.range;
    import static java.lang.System.out;
    import java.util.*;  
    
    import java.awt.*;
    import javax.swing.*;
    
    public class MyTest {
        public static void main(String[] args) {
            JFrame w = new JFrame();
            w.setSize(500, 600);
            w.setLocationRelativeTo(null);
            
            Scanner in = new Scanner(System.in);
            MyPanel myPanel = new MyPanel(in.nextInt(), in.nextDouble());
            w.add(myPanel);
            
            w.setVisible(true);
        }
    }
    
    class MyPanel extends JPanel {
        public final int X = 250, Y = 250, R = 200;
        private java.util.List<Point> points = new ArrayList<>();
        
        private int N;
        private double p;
        
        public MyPanel(int N, double p) {
            this.N = N;
            this.p = p;
        }
        
        // (x , y) is the center of the circle
        public void drawCircle(Graphics g, int x, int y, int r) {
            g.drawOval(x-r, y-r, r*2, r*2);
        }
        
        public void doRandomConnection(Graphics g, double p) {
            g.setColor(Color.GRAY);
            for (int i = 0; i != points.size(); ++i) {
                for (int j = i+1; j != points.size(); ++j) {
                    // 0 <= Math.random() < 1
                    if (Math.random() < p) { 
                        g.drawLine(points.get(i).x, points.get(i).y,
                                    points.get(j).x, points.get(j).y);
                    }
                }
            }
        }
        
        public void plotsDots(Graphics g) {
            double angle = 0;
            final double STEP = 360.0 / N;
            while (angle < 360.0) {
                int x = X+(int)(Math.cos(Math.toRadians(angle))*R);
                int y = Y+(int)(Math.sin(Math.toRadians(angle))*R);
                Point point = new Point(x, y);
                points.add(point);
    
                drawCircle(g, x, y, 5);
                angle += STEP;
            }
        }
        
        public void paint(Graphics g) {        
            drawCircle(g, X, Y, R);
            plotsDots(g);
            doRandomConnection(g, p);
        }
    }
    
    class Point {
        public int x;
        public int y;
        
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

     

  • 相关阅读:
    django关闭调试信息,打开内置错误视图
    django 配置URLconf和获取值
    django模版中配置和使用静态文件方法
    django的模型类管理器-----------数据库操作的封装
    美团校招的算法题:灯泡游戏
    ccTouchBegan
    特效effects(二)
    特效effects
    CCProgressTo和CCProgressTimer
    TransitionsTest
  • 原文地址:https://www.cnblogs.com/xkxf/p/7513904.html
Copyright © 2011-2022 走看看