gpt4 book ai didi

algorithm - 沃克三角学谜题

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:53:51 24 4
gpt4 key购买 nike

整个星期,我一直在努力解决我在编码平台上对这个练习的解决方案有什么问题。在这一点上,这不是试图在那里获得分数,而是更多地了解我的解决方案/推理中哪里错了。

这是作业:

The walker starts from point O, walks along OA, AB and BC. When he is in C (C will be in the upper half-plane), what is the distance CO? What is the angle tOC in positive degrees, minutes, seconds?

Angle tOA is alpha (here 45 degrees), angle hAB is beta (here 30 degrees), angle uBC is gamma(here 60 degrees).

Task function solve(a, b, c, alpha, beta, gamma) with parameters

a, b, c: positive integers in units of distance alpha, beta, gamma: positive integers in degrees (positive angles are anticlockwise) returns an array

first element: distance CO (rounded to the nearest integer) then angle tOC with the third following elements: second element of the array: number of degrees in angle tOC (truncated positive integer) third element of the array: number of minutes in angle tOC (truncated positive integer) fourth element of the array: number of seconds in angle tOC (truncated positive integer)

这是一张展示上述内容的小图片: enter image description here

基本上每个步骤都会创建一个矩形三角形。我的方法是使用正弦规则逐步找到 A、B 和 C 的坐标,然后找到 CO 和角度 COt 就很容易了。根据测试,我得到了正确的 CO 段,但角度似乎不对:

Test Failed
Expected Array(15, 135, 49, 18), but got Array(15, 143, 2, 45)

Test Failed
Expected Array(20, 141, 4, 23), but got Array(20, 139, 51, 47)

下面我粘贴我的解决方案和网站提供的测试用例:

object Walker {

case class Point(x: Double, y: Double){
def moveLeft(by: Double) = Point(by - x, y)
def moveRight(by: Double) = Point(by + x, y)
def moveUp(by: Double) = Point(x, by + y)
def moveDown(by: Double) = Point(x, by - y)
def inverse() = Point(y, x)
}

implicit def roundUp(d: Double): Int = math.ceil(d).toInt

def solve(a: Int, b: Int, c: Int, alpha: Int, beta: Int, gamma: Int): Array[Int] = {
val pointA = pointHypotenuse(a, alpha)
val pointB = pointHypotenuse(b, beta).inverse().moveLeft(pointA.x).moveUp(pointA.y)
val pointC = pointHypotenuse(c, gamma).moveLeft(pointB.x).moveDown(pointB.y)
val coHypotenuse: Int = math.sqrt(math.pow(pointC.x, 2) + math.pow(pointC.y, 2))
val sinC = math.sin(math.abs(pointC.x)/coHypotenuse)
val tOC = 180 - sinC.toDegrees
coHypotenuse +: degrees(tOC)
}

def pointHypotenuse(coteHypotenuse: Int, angleHypotenuse: Int): Point = {
val sinDuAngle = math.sin(math.toRadians(angleHypotenuse))
val coteOppose = sinDuAngle * coteHypotenuse
Point(math.sqrt(math.pow(coteHypotenuse, 2) - math.pow(coteOppose, 2)), coteOppose)
}

def degrees(deg: Double): Array[Int] = {
Stream.iterate((deg.toInt, deg - deg.toInt)){
case (_, r) =>
val by60 = r*60
(by60.toInt, by60 - by60.toInt)
}.map(_._1).take(3).toArray
}
}

测试(来自平台):

class WalkerTest extends FlatSpec { 
it should "pass basic tests" in {
dotest(12, 20, 18, 45, 30, 60, Array(15, 135, 49, 18))
dotest(15,15,19,50,29,55, Array(12, 133, 18, 44))
dotest(14,25,17,41,35,59, Array(20, 129, 41, 57))

}
}

object WalkerTest {
private def dotest(a: Int, b: Int, c: Int, aa: Int, bb: Int, cc: Int, expect: Array[Int]): Unit = {
val actual: Array[Int] = Walker.solve(a, b, c, aa, bb, cc)
assertResult(expect){actual}
}
}

我不认为这是一个舍入错误(用整数距离替换所有 double 来自断言)。我通常不会问这个,但我似乎真的被阻止了。

最佳答案

线

val sinC = math.sin(math.abs(pointC.x)/coHypotenuse)

得到一些段比率的正弦值,但这个比率已经是余弦了。
要获得角度,应该使用 arccos

关于algorithm - 沃克三角学谜题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51794879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com