Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 4f191db

Browse files
aQuaaQua
authored andcommitted
478 wrong answer
1 parent ef3730a commit 4f191db

File tree

5 files changed

+159
-43
lines changed

5 files changed

+159
-43
lines changed

.vscode/bookmarks.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"label": ""
99
}
1010
]
11+
},
12+
{
13+
"path": "$ROOTPATH$/Algorithms/0478.generate-random-point-in-a-circle/generate-random-point-in-a-circle.go",
14+
"bookmarks": [
15+
{
16+
"line": 29,
17+
"column": 11,
18+
"label": ""
19+
}
20+
]
1121
}
1222
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [478. Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/)
2+
3+
## 题目
4+
5+
Given the radius and x-y positions of the center of a circle, write a function `randPoint` which generates a uniform randompoint in the circle.
6+
7+
Note:
8+
9+
1. input and output values are in [floating-point](https://www.webopedia.com/TERM/F/floating_point_number.html).
10+
1. radius and x-y position of the center of the circle is passed into the class constructor.
11+
1. a point on the circumference of the circle is considered to bein the circle.
12+
1. randPointreturnsa size 2 array containing x-position and y-position of the random point, in that order.
13+
14+
Example 1:
15+
16+
```text
17+
Input:
18+
["Solution","randPoint","randPoint","randPoint"]
19+
[[1,0,0],[],[],[]]
20+
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]
21+
```
22+
23+
Example 2:
24+
25+
```text
26+
Input:
27+
["Solution","randPoint","randPoint","randPoint"]
28+
[[10,5,-7.5],[],[],[]]
29+
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]
30+
```
31+
32+
Explanation of Input Syntax:
33+
34+
The input is two lists:the subroutines calledand theirarguments.Solution'sconstructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments.Argumentsarealways wrapped with a list, even if there aren't any.
35+
36+
## 解题思路
37+
38+
见程序注释
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package problem0478
2+
3+
import (
4+
"math"
5+
"math/rand"
6+
)
7+
8+
// Solution object will be instantiated and called as such:
9+
// obj := Constructor(radius, x_center, y_center);
10+
// param_1 := obj.RandPoint();
11+
type Solution struct {
12+
r, a, b, ratio float64
13+
}
14+
15+
// Constructor 构建 Solution
16+
func Constructor(radius, xCenter, yCenter float64) Solution {
17+
ratio := 1.
18+
if radius < 1 {
19+
radius = 1.
20+
ratio = 1. / radius
21+
}
22+
return Solution{
23+
r: radius,
24+
a: xCenter,
25+
b: yCenter,
26+
ratio: ratio,
27+
}
28+
}
29+
30+
// RandPoint 返回随机点
31+
func (s *Solution) RandPoint() []float64 {
32+
rjd := rand.Float64()
33+
x := s.a + s.r*math.Sin(rjd)
34+
y := s.b + s.r*math.Cos(rjd)
35+
return []float64{x / s.ratio, y / s.ratio}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problem0478
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_RandPoint(t *testing.T) {
11+
ast := assert.New(t)
12+
r, a, b := 3., 2., 1.
13+
s := Constructor(r, a, b)
14+
for i := 0; i < 100; i++ {
15+
p := s.RandPoint()
16+
x, y := p[0], p[1]
17+
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
18+
ast.InDelta(r, actual, 0.00001)
19+
}
20+
}
21+
22+
func Test_RandPoint_2(t *testing.T) {
23+
ast := assert.New(t)
24+
r, a, b := 0.01, -73839.1, -3289891.
25+
s := Constructor(r, a, b)
26+
for i := 0; i < 100; i++ {
27+
p := s.RandPoint()
28+
x, y := p[0], p[1]
29+
actual := math.Sqrt((x-a)*(x-a) + (y-b)*(y-b))
30+
ast.InDelta(r, actual, 0.00001)
31+
}
32+
}

0 commit comments

Comments
 (0)