使用蒙特卡洛仿真方法求圆周率。
输入格式:
从键盘输入四个实型数和一个整型数,分别为矩形左上角的横坐标、纵坐标、矩形长度、矩形宽度和投点次数,数与数之间可以用一个或多个空格或回车分隔。
输出格式:
如果矩形长度与宽度不相等(非正方形)或长宽数据非法,则输出“Wrong Format”。
如果估算出的π与Math.PI差值小于1E-4,则输出“Success”,否则输出“failed”。
输入样例:
在这里给出一组输入。例如:
0 0 1 1 20000000
输出样例:
在这里给出相应的输出。例如:
Success
这里就自主参透,注释有些复杂
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double abscissa,ordinate;
double length,width;
int count = 0;
Scanner input = new Scanner(System.in);
abscissa = input.nextDouble();
ordinate = input.nextDouble();
length = input.nextDouble();
width = input.nextDouble();
count = input.nextInt();
Rectangle rectangle = new Rectangle(new Coordinate(abscissa,ordinate),length,width);
MonteCarloSimulation monteCarlo = new MonteCarloSimulation(rectangle);
if(monteCarlo.validateRectangle()){
monteCarlo.setCircle();
if((Math.abs(monteCarlo.simulation(count) - Math.PI)) <= 1e-3){
System.out.println("Success");
}
else{
System.out.println("failed");
}
}else{
System.out.println("Wrong Format");
}
}
}
class MonteCarloSimulation{
private Rectangle rectangle;
private Circle c;
public MonteCarloSimulation(Rectangle rectangle) {
this.rectangle = rectangle;
}
public void setCircle(){
this.c=new Circle(rectangle);
}
public boolean validateRectangle(){//验证矩形
boolean ret=false;
if (rectangle.getLength()== rectangle.getWidth()) ret=true;
return ret;
}
public double simulation(int count){//模拟
int num=0;
int numx=0;
Random rand=new Random();
for (int i = 0; i < count; i++) {
//根据样例来,样例的随机数范围是[0,1),不包括1的那个边界。
double x= rand.nextDouble()*(rectangle.getWidth())+rectangle.getCoordinate().getAbscissa();
double y= rand.nextDouble()*(rectangle.getLength())+rectangle.getCoordinate().getOrdinate()-rectangle.getLength();
//测试数据
if (x>=rectangle.getCoordinate().getAbscissa()&&x<=rectangle.getCoordinate().getAbscissa()+ rectangle.getWidth()){
numx++;
}
//测试数据结束
if (Math.pow(x-c.getCenterOfCircleX(),2)+Math.pow(y-c.getCenterOfCircleY(),2)<=Math.pow(c.getCenterOfCircleR(),2)){
num++;
}
}
//System.out.println(numx);
//System.out.println(num*4.0/count);
return num*4.0/count;
}
public static class Circle{
private double centerOfCircleX;
private double centerOfCircleY;
private double centerOfCircleR;
public Circle(Rectangle rectangle) {
this.centerOfCircleX = rectangle.getCoordinate().getAbscissa()+rectangle.getWidth()/2.0;
this.centerOfCircleY = rectangle.getCoordinate().getOrdinate()-rectangle.getLength()/2.0;
this.centerOfCircleR=Math.abs(rectangle.getCoordinate().getAbscissa()-centerOfCircleX);
}
public double getCenterOfCircleX() {
return centerOfCircleX;
}
public double getCenterOfCircleY() {
return centerOfCircleY;
}
public double getCenterOfCircleR() {
return centerOfCircleR;
}
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
}
class Coordinate{
private double abscissa;
private double ordinate;
public Coordinate(double abscissa, double ordinate) {
this.abscissa = abscissa;
this.ordinate = ordinate;
}
public double getAbscissa() {
return abscissa;
}
public void setAbscissa(double abscissa) {
this.abscissa = abscissa;
}
public double getOrdinate() {
return ordinate;
}
public void setOrdinate(double ordinate) {
this.ordinate = ordinate;
}
}
class Rectangle{
private Coordinate coordinate;
private double length,width;
public Rectangle(Coordinate coordinate, double length, double width) {
this.coordinate = coordinate;
this.length = length;
this.width = width;
}
public Coordinate getCoordinate() {
return coordinate;
}
public void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}