- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
整个星期,我一直在努力解决我在编码平台上对这个练习的解决方案有什么问题。在这一点上,这不是试图在那里获得分数,而是更多地了解我的解决方案/推理中哪里错了。
这是作业:
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)
基本上每个步骤都会创建一个矩形三角形。我的方法是使用正弦规则逐步找到 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/
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 7 年前。 我正在尝试解开这个谜团: in
很难为这个问题找到合适的标题。欢迎编辑! :) 这是我的来源xml代码: [color hex] [color hex] [color he
编辑:看起来像一个索引问题,在问题底部更新 我有以下查询 + 子查询,其结果我无法解释。我从这个最小的输入数据集开始(这里的应用程序正在捕获数据变化,PK 是 id + tx_id)。 mysql>
基本上,我在这里试图实现的是让全局变量具有指向结构的指针数组,其大小在编译时是未知的——在我下面的示例中,它是 my_struct **tab。在最终版本中,我想调用一个 JNI 方法来初始化我的指针
所以我想弄清楚这个谜题: function fun1(){ var result = []; for (var i = 0; i < 5; i++){ result.p
一群 child 围成一圈。选择第一个 child ,他们从那个 child 开始顺时针计数,直到达到固定数字(n,在游戏开始时给出)。当计数达到 n 时,第 n 个位置的 child 被淘汰。游戏从
(我是 JS 新手,所以请耐心等待)我正在使用 table 来构建滑动益智游戏。我需要一个可以扰乱值的函数,但我不确定应该如何让它显示在表格单元格中。现在我的代码只是按顺序显示数字。 我有两个函数 -
我有一个 UserForm,xForm,它在类模块(假设为 TestClass)中实例化为: 'TestClass Dim Form as New xForm Private WithEvents E
如果没有循环或游标,如何获取日期间隔列表并将它们转换为 1 和 0 的字符串,这样: 每一位代表从 min(所有日期)到 max(所有日期)的每一天 如果该天属于任何日期间隔,则该位为 1 如果该天不
我读过很多A*算法的伪代码,但它们都没有真正解释如何输出解。我相信我理解使用优先级队列表示尚未访问的内容和使用已探索的表的概念,但是当我执行该算法时,我不知道在什么时候打印出结果。有没有人有一个伪代码
以下两个查询不会返回相同的结果。为什么? 注意 :我发现这个问题是一个 Mysql 难题,我没有关于这个问题的更多数据? SELECT table1.* FROM table1 LEFT JOIN t
如果您对我应该如何在 python 中执行以下任务有任何建议,我正在徘徊:假设我有以下类(class): class A(object): self._classes = [] def
我很难理解如何在不引起内存分配问题的情况下解决这个问题。我很确定我的逻辑是合理的,但不幸的是,这似乎还不够好。有没有人对此有任何建议,以便我了解如何更有效地编写代码? 问题来了:示例输入:1个5 2
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
所以这是一个难题: “kb”是扩展 java.util.Hashtable 的类的实例键是一个字符串,存储的值是一个名为“IntelCard”的类 此代码提取 key ,并尝试从表中打印数据
我有以下难题要解决,但我不确定我该怎么做。它说: 有一个 Ubuntu Linux C 程序可以输出变量的地址。 v1: 0xa156128 v2: 0xff97410c v3: 0xf750e34b
我有一个简单的 HTML 页面如下:- Col1 Col2
大家好, 我尝试了八个难题的解决方案发布 here由 joel Neely 玩弄并修改它,以便可以用来解决更高的网格[将网格的字符串表示更改为二维整数表示并修改相应的逻辑]。然而,修改后的代码可以解决
我正在尝试解决下文详述的 projecteuler 难题。我当前的函数适用于数字 1 到 10,但是当我尝试 1 到 20 时,它会一直循环下去而没有结果。 2520 is the smallest
我在为基于图 block 的游戏编写随机关卡生成器时遇到了一个有趣的问题。我已经为它实现了一个强力求解器,但它的速度呈指数级下降,而且绝对不适合我的用例。我不一定要寻找完美的解决方案,我会对性能良好的
我是一名优秀的程序员,十分优秀!