The Angles of a Triangle
The Angles of a Triangle
You are given the lengths for each side on a triangle. You need to find all three angles for this triangle. If the given side lengths cannot form a triangle (or form a degenerated triangle), then you must return all angles as 0 (zero). The angles should be represented as a list of integers in ascending order. Each angle is measured in degrees and rounded to the nearest integer number (Standard mathematical rounding).
Input: The lengths of the sides of a triangle as integers.
Output: Angles of a triangle in degrees as sorted list of integers.
原题链接: http://www.checkio.org/mission/triangle-angles/
题目大义: 已知三角形三边长, 求出各个角度, 如果无法构成一个三角形返回[0, 0, 0]
思路: 余弦定理
import math def checkio(a, b, c):
rel = [0, 0, 0]
if a + b > c and a + c > b and b + c > a:
rel[0] = int(round(math.degrees(math.acos(float(a**2 + b**2 - c**2) / float(2 * a * b)))))
rel[1] = int(round(math.degrees(math.acos(float(a**2 + c**2 - b**2) / float(2 * a * c)))))
rel[2] = int(round(math.degrees(math.acos(float(b**2 + c**2 - a**2) / float(2 * b * c))))) rel = sorted(rel) return rel
当然, 如果求出前两个角之后, 最后一个角可以通过180 - rel[0] - rel[1]得到; 若首先对a, b, c进行排序, 也可以进一步优化程序
import math def checkio(a, b, c):
edge = sorted([a, b, c]) if edge[0] + edge[1] <= edge[2]:
return [0, 0, 0] rel = [0, 0, 0]
rel[0] = int(round(math.degrees(math.acos(float(edge[2]**2 + edge[1]**2 - edge[0]**2) / float(2 * edge[2] * edge[1])))))
rel[1] = int(round(math.degrees(math.acos(float(edge[2]**2 + edge[0]**2 - edge[1]**2) / float(2 * edge[2] * edge[0])))))
rel[2] = 180 - rel[0] - rel[1] return rel
The Angles of a Triangle的更多相关文章
- <<Differential Geometry of Curves and Surfaces>>笔记
<Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...
- The Hundred Greatest Theorems
The Hundred Greatest Theorems The millenium seemed to spur a lot of people to compile "Top 100& ...
- <Differential Geometry of Curves and Surfaces>(by Manfredo P. do Carmo) Notes
<Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...
- hdu 4082 Hou Yi's secret(暴力枚举)
Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Digital filter
https://ww2.mathworks.cn/help/signal/examples/practical-introduction-to-digital-filter-design.html D ...
- UVa OJ 194 - Triangle (三角形)
Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...
- A. Srdce and Triangle 几何题
链接:https://www.nowcoder.com/acm/contest/104/A来源:牛客网 题目描述 Let be a regualr triangle, and D is a poin ...
- Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)
Triangle 一个二维高质量网格(mesh)生成器和Delaunay三角化工具. PSLG(Planar Straight Line Graph)约束Delaunay三角网(CDT)与Delaun ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- PHP null常量和null字节的区别
在学习isset()时,看到了这句话:“如果已经使用 unset() 释放了一个变量之后,它将不再是 isset().若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE.同 ...
- Git本地版本控制备忘
首先git是一个版本控制工具,类似于SVN 笔记包括两部分,git本地版本控制和git远程协助 一.Git本地版本控制 以git windows版本msysgit为例,下载地址http://msysg ...
- 线性时间构造普吕弗(Prüfer)序列
tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...
- socket pro
/etc/exports/tmp目录共享为任何人可以共享并可以进行读写操作 /tmp *(rw,no_root_squash) /home/test 192.168.1.*(rw) *(ro) /et ...
- PHP常用魔术方法(__toString魔术方法)
<?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload_register('\\ ...
- 【转】修改Android工程的名称、安装路径
申明:转载自:http://blog.csdn.net/hcj116/article/details/21712353 在Google提供的Eclipse集成开发环境adt-bundle下修改名称的总 ...
- Struts2 页面url请求怎样找action
1.我们使用最原始的方法去查找action.不同注解. struts.xml文件先配置 <!-- 新闻信息action --> <action name="newsInfo ...
- Android 关于倒计时功能的实现
关于倒计时的实现,可以说有很多的方法,比较常见的就是Timer+TimerTask+Handler了,或者还可以配合Runnable.例如下面的代码: import java.util.Timer; ...
- JavaScript 公有 私有 静态属性和方法
1.公有属性和公有方法 这里的 name age 都是参数传递进去 可以在外面直接实例化调用. 2.私有属性和方法 私有的只能在函数内部使用 作用域的原因 3.静态属性和静态方法 这里我首先 创建 ...
- samba后台进程及安全模式简介
识别 Samba 后台程序Linux 服务器通常作为守护程序(daemon) 来实现,这一词源于希腊神话,其中守护神(daemon)是超自然生物.Linux 守护程序在后台运行以便执行一些有用的任务. ...