生活随笔
收集整理的這篇文章主要介紹了
7-35 蒙特卡罗方法求圆周率 (30 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用蒙特卡洛仿真方法求圓周率。
輸入格式:
從鍵盤輸入四個實型數和一個整型數,分別為矩形左上角的橫坐標、縱坐標、矩形長度、矩形寬度和投點次數,數與數之間可以用一個或多個空格或回車分隔。
輸出格式:
如果矩形長度與寬度不相等(非正方形)或長寬數據非法,則輸出“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
++) {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
++;}}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
;}
}
總結
以上是生活随笔為你收集整理的7-35 蒙特卡罗方法求圆周率 (30 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。