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的更多相关文章

  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. Linux 删除空行

    在Linux上处理一些数据文件时,有时候需要将其中的空行过滤掉,系统中提供的各种工具都可以完成这个功能.将常用的介绍如下吧:1. grep grep . data.txt grep -v '^$' d ...

  2. bzoj1145

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1145 神题...... 定义f(abcd)为高度排名为abcd的个数,例如闪电的个数为f(13 ...

  3. thinkphp连接mysql5.5版本数据库

    //数据库配置信息 'DB_TYPE' => 'mysqli', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => ' ...

  4. c语言通过时间种子产生随机数并选出最大值以及下标

    1 #include <stdio.h> #include <stdlib.h> #include <time.h> //2016 10 10 void main( ...

  5. PyQt实现图片中心旋转

    # -*- coding: cp936 -*- from PyQt4 import QtCore, QtGui, Qt class RotatePage(QtGui.QFrame): def __in ...

  6. java 版本SQLHelper

    package com.jack.SQLHelper; import java.sql.*;import java.util.logging.*;import javax.swing.table.*; ...

  7. 深入浅出理解iOS经常使用的正則表達式—基础篇[Foundation]

    參考资料:cocoachina的zys475481075的文章 几个单词 Regular ['regjʊlə]adj. 定期的:有规律的 Expression[ɪk'spreʃ(ə)n; ek-] n ...

  8. linux服务器加入windows域时报错Ticket expired

    [root@rusky]# net ads join -U administrator Enter administrator's password: kinit succeeded but ads_ ...

  9. bit、byte、位、字节、字符串等概念

    原始文章:http://djt.qq.com/article/view/658 1.古代送信:马车,烽火,信鸽 2.1837年,世界第一条电报诞生, 美国科学家莫尔斯尝试用一些“点”和“划”来表示不同 ...

  10. DEV LookUpEdit 使用方法

    public class field { public string Name { get; set; } public string Explain { get; set; } } List< ...