随机算法的应用

2018-10-27  本文已影响0人  jiamjg

用于计算概率,无需通过复杂的数学公式进行具体场景的概率计算,只需用随机数模拟出相关场景,即可得到对应概率。

计算生日重复的概率

计算30个人的班级,生日出现重复的概率。

代码如下:

public class 随机算法1_计算重复生日的概率 {
    public static void main(String[] args) {
        int N = 1000 * 10;
        int n = 0;
        //0-365随机产生数字,有没有碰撞
        for (int i = 0; i < N; i++) {
            int[] x = new int[365];
            for (int j = 0; j < 30; j++) {
                int y = (int) (Math.random() * 365);
                if (x[y] == 1) {
                    n++;
                    break;
                } else {
                    x[y] = 1;
                }
            }
        }
        double rate = (double) n / N;
        System.out.println(rate);
    }
}

24点

给四张扑克牌,点数 1 ~ 10
用 + - * / 运算,使得最后结果为 24
代码如下:

import java.util.Scanner;
import java.util.Stack;

public class 随机算法2_24点 {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String[] ss = sc.nextLine().split(" ");
        f(ss);
    }

    private static void f(String[] ss) throws Exception {
        for (int i = 0; i < (1000 * 100); i++) {
            String[] buf = new String[7];     //4张牌,3个运算符;
            for (int j = 0; j < 4; j++) {
                buf[j] = ss[j];        //给4张牌赋值
            }
            for (int j = 4; j < 7; j++) {
                buf[j] = createSymol();
            }
            shuffle(buf);
            if (caculate(buf)) {
                show(buf);
                break;
            }
        }
    }

    private static String createSymol() {
        int n = (int) (Math.random() * 4);
        if (n == 0) {
            return "+";
        }
        if (n == 1) {
            return "-";
        }
        if (n == 2) {
            return "*";
        }
        return "/";
    }

    //洗牌
    private static void shuffle(String[] x) {
        for (int i = 0; i < x.length; i++) {
            int index = (int) (Math.random() * x.length);
            String temp = x[i];
            x[i] = x[index];
            x[index] = temp;
        }
    }

    //计算
    private static boolean caculate(String[] data) throws Exception {
        Stack s = new Stack();
        try {
            for (int i = 0; i < data.length; i++) {
                if (data[i].equals("+") || data[i].equals("-") || data[i].equals("*") || data[i].equals("/")) {
                    int a = Integer.parseInt(s.pop().toString());
                    int b = Integer.parseInt(s.pop().toString());
                    s.push(caculate_detail(a, b, data[i]) + "");
                } else {
                    s.push(data[i]);
                }
            }
        } catch (Exception e) {
            return false;
        }
        if (s.size() == 1 && s.pop().equals("24")) {
            return true;
        }
        return false;
    }

    private static int caculate_detail(int a, int b, String operator) throws Exception {
        if (operator.equals("+")) {
            return a + b;
        }
        if (operator.equals("-")) {
            return a - b;
        }
        if (operator.equals("*")) {
            return a * b;
        } else {
            if (a % b != 0) {
                throw new Exception("can not /");
            } else {
                return a / b;
            }
        }
    }

    //结果
    private static void show(String[] data) {
        Stack s = new Stack();
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals("+") || data[i].equals("-") || data[i].equals("*") || data[i].equals("/")) {
                s.push("(" + s.pop() + data[i] + s.pop() + ")");
            } else {
                s.push(data[i]);
            }
        }
        System.out.println(s.pop());
    }
}
上一篇下一篇

猜你喜欢

热点阅读