日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

7-35 蒙特卡罗方法求圆周率 (30 分)

發布時間:2024/8/5 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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++) {//根據樣例來,樣例的隨機數范圍是[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;} }

總結

以上是生活随笔為你收集整理的7-35 蒙特卡罗方法求圆周率 (30 分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。