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 ...
随机推荐
- EBS导出键弹性域
select gl_flexfields_pkg.get_description_sql(gcc.chart_of_accounts_id, 1, gcc.segment1) || '-' || gl ...
- poj1185:炮兵阵地(状压dp)
也算是比较基础的状压dp了,跟做过的第二道比较又稍微复杂了一点 需要记录之前两行的状态.. 统计结果也稍有不同 另外还学习了一个得到一个整数二进制位 1 的个数的位运算方法 详见代码: #includ ...
- 找出最小的k个数
•已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...
- MYSQL中的语句
MYSQL中的语句 decimal(8,2):最多存10位数的数字,小数点后保存两位.如:999999.99
- (转)iOS Wow体验 - 第四章 - 为应用的上下文环境而设计
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第四章译文精选,其余章节将陆续放出.上一篇:Wow ...
- Git冲突解决方案
Git冲突解决方案 1. 在代码提交时,先更新,若有冲突.先解决冲突.若提交之后在review时才发现无法合并代码时有冲突,需要abandon此次提交的代码. 2. 解决冲突的基本做法,保存本地代 ...
- MySQL server version for the right syntax to use near 'type=InnoDB' at line 1
转载请注明出处:http://blog.csdn.net/bettarwang/article/details/40180271 在执行一个Hibernate的演示样例时,配置了<propert ...
- [Angular 2] Using ng-for to repeat template elements
This lesson covers Angular 2’s version of looping through data in your templates: ng-for. It’s conce ...
- fread(),fwrite() 读/写流
C 库函数 - fread() 描述 C 库函数 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 从给定流 strea ...
- ASP.NET 根据现有动态页面生成静态Html
现有动态页面的格式都是类似 pageName.aspx?ID=1的格式,后面由于发布服务器的原因,要求将动态页面转为静态html后上传. 首先根据页面生成的格式,枚举获取页面html: foreach ...