蒙特卡罗方法求圆周率--PTA

使用蒙特卡洛仿真方法求圆周率。
题目具体要求请点击查看
输入格式:
从键盘输入四个实型数和一个整型数,分别为矩形左上角的横坐标、纵坐标、矩形长度、矩形宽度和投点次数,数与数之间可以用一个或多个空格或回车分隔。

输出格式:
如果矩形长度与宽度不相等(非正方形)或长宽数据非法,则输出“Wrong Format”。
如果估算出的π与Math.PI差值小于1E-4,则输出“Success”,否则输出“failed”。
输入样例:
在这里给出一组输入。例如:

0 0 1 1 20000000

输出样例:
在这里给出相应的输出。例如:

Success

测试点如下:
在这里插入图片描述
参考代码如下:

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();
	Coordinate coordinate=new Coordinate(abscissa,ordinate);
	Rectangle rectangle = new Rectangle(coordinate,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 Coordinate{
	private double abscissa;
	private double ordinate;
	Coordinate(){
		
	}
	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{
	 Coordinate coordinate;
	private double width,length;
	Rectangle(){
		
	}
	Rectangle(Coordinate coordinate,double width,double length){
		this.length=length;
		this.width=width;
		this.coordinate=coordinate;
	}
	public Coordinate getCoordinate() {
		return coordinate;
	}
	public void setCoordinate(Coordinate coordinate) {
		this.coordinate=coordinate;
	}
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width=width;
	}
	public double getLength() {
		return length;
	}
	public void setLength(double length) {
		this.length=length;
	}
}
class Circle{
	Coordinate coordinate;
	private double radius;
	Circle(){
		
	}
	Circle(Coordinate coordinate,double radius){
		this.coordinate=coordinate;
		this.radius=radius;
	}
	public Coordinate getCoordinate() {
		return coordinate;
	}
	public void setCoordinate(Coordinate coordinate) {
		this.coordinate=coordinate;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius=radius;
	}
}
class MonteCarloSimulation{
	 Rectangle rectangle;
	 Circle circle;
	MonteCarloSimulation(){
		
	}
	MonteCarloSimulation(Rectangle rectangle,Circle circle){
		this.circle=circle;
		this.rectangle=rectangle;
	}
	MonteCarloSimulation(Rectangle rectangle){
		this.rectangle=rectangle;
	}
	public Rectangle getRectangle() {
		return rectangle;
	}
	public void setRectangle(Rectangle rectangle) {
		this.rectangle=rectangle;
	}
	public Circle getCircle() {
		return circle;
	}
	public void setCircle(Circle circle) {
		this.circle=circle;
	}
	public boolean validateRectangle() {
		if(rectangle.getLength()!=rectangle.getWidth()||rectangle.getWidth()<=0||rectangle.getWidth()<=0)
			return false;
		else return true;
	}
	public void setCircle() {
	
	}
	public double simulation(int n) {
		int m=0;
		double x=(2*rectangle.getCoordinate().getAbscissa()+getRectangle().getWidth())/2;
		double y=(2*rectangle.getCoordinate().getOrdinate()-getRectangle().getWidth())/2;
		//System.out.println(x);
		//System.out.println(y);
		double radius=this.getRectangle().getWidth()/2;
	//	System.out.println(radius);
		for(int i=0;i<n;i++) {
		double x0=Math.random()*(getRectangle().getWidth())+rectangle.getCoordinate().getAbscissa();
		double y0=Math.random()*(getRectangle().getWidth())+(rectangle.getCoordinate().getOrdinate()-getRectangle().getWidth());
		//System.out.println(x0);
		//System.out.println(y0);
		if((x-x0)*(x-x0)+(y-y0)*(y-y0)<=radius*radius)
			m++;
		}
		//System.out.println(4.0*m/n);
		return 4.0*m/n;
	}
}
--------代码为作者原创,引用请注明出处----谢谢!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值