基本信息
源码名称:看得见的算法(java源码)-7个经典应用诠释算法精髓
源码大小:2.35M
文件格式:.zip
开发语言:Java
更新时间:2020-02-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
看得见的算法 7个经典应用诠释算法精髓源码

02-Java-Swing-Basics
03-Probability-Simulation
04-Sort-Visualization
05-Maze-Solver
06-Maze-Generalization
07-Mine-Sweeper
08-Move-the-Box-Solver
09-Fractal-Drawing

import java.awt.*;

public class MonteCarloExperiment {

    private int squareSide;
    private int N;
    private int outputInterval = 100;

    public MonteCarloExperiment(int squareSide, int N){

        if(squareSide <= 0 || N <= 0)
            throw new IllegalArgumentException("squareSide and N must larger than zero!");

        this.squareSide = squareSide;
        this.N = N;
    }

    public void setOutputInterval(int interval){

        if(interval <= 0)
            throw new IllegalArgumentException("interval must be larger than zero");

        this.outputInterval = interval;
    }

    public void run(){

        Circle circle = new Circle(squareSide/2, squareSide/2, squareSide/2);
        MonteCarloPiData data = new MonteCarloPiData(circle);

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

            if( i % outputInterval == 0) {
                System.out.println(data.estimatePi());
            }

            int x = (int)(Math.random() * squareSide);
            int y = (int)(Math.random() * squareSide);
            data.addPoint(new Point(x, y));
        }
    }

    public static void main(String[] args) {

        int squareSide = 800;
        int N = 10000000;

        MonteCarloExperiment exp = new MonteCarloExperiment(squareSide, N);
        exp.setOutputInterval(100000);
        exp.run();
    }
}