• Java实现 LeetCode 478 在圆内随机生成点


    478. 在圆内随机生成点

    给定圆的半径和圆心的 x、y 坐标,写一个在圆中产生均匀随机点的函数 randPoint 。

    说明:

    输入值和输出值都将是浮点数。
    圆的半径和圆心的 x、y 坐标将作为参数传递给类的构造函数。
    圆周上的点也认为是在圆中。
    randPoint 返回一个包含随机点的x坐标和y坐标的大小为2的数组。
    示例 1:

    输入:
    [“Solution”,“randPoint”,“randPoint”,“randPoint”]
    [[1,0,0],[],[],[]]
    输出: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]
    示例 2:

    输入:
    [“Solution”,“randPoint”,“randPoint”,“randPoint”]
    [[10,5,-7.5],[],[],[]]
    输出: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]
    输入语法说明:

    输入是两个列表:调用成员函数名和调用的参数。Solution 的构造函数有三个参数,圆的半径、圆心的 x 坐标、圆心的 y 坐标。randPoint 没有参数。输入参数是一个列表,即使参数为空,也会输入一个 [] 空列表。

    class Solution {
    
       private double radius;
        private double x_center;
        private double y_center;
        public Solution(double radius, double x_center, double y_center) {
            this.radius = radius;
            this.x_center = x_center;
            this.y_center = y_center;
        }
        
        public double[] randPoint() {
            Random random = new Random();
            double x = x_center-radius + 2*radius*random.nextDouble();
            double y = y_center-radius + 2*radius*random.nextDouble();
            while(Math.sqrt(Math.pow(x - x_center, 2) + Math.pow(y - y_center, 2)) > radius){
                x = x_center-radius + 2*radius*random.nextDouble();
                y = y_center-radius + 2*radius*random.nextDouble();
            }
            return new double[]{x, y};
        }
    
       
    }
    
    /**
     * Your Solution object will be instantiated and called as such:
     * Solution obj = new Solution(radius, x_center, y_center);
     * double[] param_1 = obj.randPoint();
     */
    
  • 相关阅读:
    Poj 3264 Balanced Lineup RMQ模板
    Poj 3294 Life Forms (后缀数组 + 二分 + Hash)
    Poj 1743 Musical Theme (后缀数组+二分)
    Poj 2774 Long Long Message (后缀数组)
    Poj 3436 ACM Computer Factory (最大流)
    Hdu 4465 Candy (快速排列组合+概率)
    Hdu 3605 Escape (最大流 + 缩点)
    Hdu 4292 Food (最大流)
    Hdu 5416 CRB and Tree (bfs)
    Hdu 5407 CRB and Candies (找规律)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075023.html
Copyright © 2020-2023  润新知