java算法应用_看得见的算法(java源码)-7个经典应用诠释算法精髓

【实例简介】看得见的算法 7个经典应用诠释算法精髓源码

【实例截图】

137b3fbb59aa90ba6a775d918238b62a.png

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值