美文网首页
随机算法的应用

随机算法的应用

作者: jiamjg | 来源:发表于2018-10-27 22:29 被阅读0次

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

计算生日重复的概率

计算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());
    }
}

相关文章

  • 随机算法的应用

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

  • 随机算法

    0.目录 1.随机算法 2.随机数发生器 3.随机算法的应用3.1 跳跃表3.1-1 跳跃表引申——1-2-3确定...

  • 第二十二节-哈希算法(下)

    这节讲的主要是哈希算法在分布式系统中的应用 应用五:负载均衡 负载均衡的算法很多,有轮询、随机、加权轮询等。但要实...

  • 随机森林算法描述及应用

    --2019-12-09随机森林算法描述及应用 链接:https://yq.aliyun.com/articles...

  • 基于深度学习的安卓恶意软件检测(二)

    在上一节中介绍了安卓恶意应用检测的背景知识,本节将介绍项目用到的传统机器学习算法和深度学习算法. 1 随机森林算法...

  • 多层神经网络,从零开始——(九)、优化函数

    常用的优化算法有:随机梯度下降、带动量的随机梯度下降、AdaGrad算法、RMSProp算法、Adam算法,其中A...

  • 集成学习之Bagging和RF

    一、什么是随机森林 二、随机森林的两个随机 三、随机森林算法过程 四、为什么如此受欢迎 五、随机森林算法的优缺点 ...

  • 概率分析与随机算法

    目录 0.雇佣问题 1.概率分析的含义 2.随机算法 3.随机算法与概率分析的区别 4.雇佣问题的随机算法4.1 ...

  • 机器学习(17)——GMM算法

    前言:介绍一下EM算法的简单应用 算法流程 先从一个简单的例子开始:随机选择1000名用户,测量用户的身高;若样本...

  • 三种迷宫生成算法概述

    1. Randomized Prim's algorithm(随机Prim算法) 随机Prim算法属于打通墙壁生成...

网友评论

      本文标题:随机算法的应用

      本文链接:https://www.haomeiwen.com/subject/npydtqtx.html