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]

思路: 余弦定理

  1. import math
  2.  
  3. def checkio(a, b, c):
  4. rel = [0, 0, 0]
  5. if a + b > c and a + c > b and b + c > a:
  6. rel[0] = int(round(math.degrees(math.acos(float(a**2 + b**2 - c**2) / float(2 * a * b)))))
  7. rel[1] = int(round(math.degrees(math.acos(float(a**2 + c**2 - b**2) / float(2 * a * c)))))
  8. rel[2] = int(round(math.degrees(math.acos(float(b**2 + c**2 - a**2) / float(2 * b * c)))))
  9.  
  10. rel = sorted(rel)
  11.  
  12. return rel

当然, 如果求出前两个角之后, 最后一个角可以通过180 - rel[0] - rel[1]得到; 若首先对a, b, c进行排序, 也可以进一步优化程序

  1. import math
  2.  
  3. def checkio(a, b, c):
  4. edge = sorted([a, b, c])
  5.  
  6. if edge[0] + edge[1] <= edge[2]:
  7. return [0, 0, 0]
  8.  
  9. rel = [0, 0, 0]
  10. rel[0] = int(round(math.degrees(math.acos(float(edge[2]**2 + edge[1]**2 - edge[0]**2) / float(2 * edge[2] * edge[1])))))
  11. rel[1] = int(round(math.degrees(math.acos(float(edge[2]**2 + edge[0]**2 - edge[1]**2) / float(2 * edge[2] * edge[0])))))
  12. rel[2] = 180 - rel[0] - rel[1]
  13.  
  14. return rel

The Angles of a Triangle的更多相关文章

  1. <<Differential Geometry of Curves and Surfaces>>笔记

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

  2. The Hundred Greatest Theorems

    The Hundred Greatest Theorems The millenium seemed to spur a lot of people to compile "Top 100& ...

  3. <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== ...

  4. hdu 4082 Hou Yi's secret(暴力枚举)

    Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Digital filter

    https://ww2.mathworks.cn/help/signal/examples/practical-introduction-to-digital-filter-design.html D ...

  6. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  7. A. Srdce and Triangle 几何题

    链接:https://www.nowcoder.com/acm/contest/104/A来源:牛客网 题目描述 Let  be a regualr triangle, and D is a poin ...

  8. Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)

    Triangle 一个二维高质量网格(mesh)生成器和Delaunay三角化工具. PSLG(Planar Straight Line Graph)约束Delaunay三角网(CDT)与Delaun ...

  9. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

随机推荐

  1. Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)

    在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManife ...

  2. Oracle中对列加密的方法

    Oracle中对列加密的方法 2011-12-22 17:21:13 分类: Linux Oracle支持多种列加密方式: 1,透明数据加密(TDE):create table encrypt_col ...

  3. bzoj1149

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1149 水题..... 直接BFS. #include<cstdio> #inclu ...

  4. 移动web开发研究

    1.jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mobile应用用户界面.jQuery Mobile最新版本是1.4.0,默认主题采用扁平化设计风格 ...

  5. Mac系统升级到10.9(mavericks)时安装php扩展问题解决(转)

    问题一: 执行执行 phpize 报错: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include ...

  6. TableView 校检表

    这俩天学习了tableView 校检表 主要就是通过一个方法来跟踪当前选中的行.下面将声明一个NSIndexPath 的属性来跟踪最后选中的行.这篇文章希望能给那些初学者带来学习的乐趣.不说了直接上代 ...

  7. MyBatis配置解析

    MyBatis配置文件解析(概要) 1.configuration:根元素 1.1 properties:定义配置外在化 1.2 settings:一些全局性的配置 1.3 typeAliases:为 ...

  8. Salt安装

    salt-master安装 [salt-master]# yum install salt-master 或者 curl -L http://bootstrap.saltstack.org | sud ...

  9. Java中Thread类的start()和run()的区别

    1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码. 通 过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪 ...

  10. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...