Is this a triangle? -- 7kyu

来源:互联网 发布:lol徐老师淘宝网站 编辑:程序博客网 时间:2024/06/01 14:35

原题http://www.codewars.com/kata/is-this-a-triangle/train/cpp

题目

Implement a method that accepts 3 integer values a, b, c. The method should return true if a triangle can be built with the sides of given length and false in any other case.
(In this case, all triangles must have surface greater than 0 to be accepted).

分析

*三边构成三角形条件:都要满足两边之和大于第三边,两边之差小于第三边并且边需要大于0,所以负数和0排除需要那么就可以推导出两种判断方式:
1.A + b> C和A + C> b和b + C> A
2.ab

代码

namespace Triangle{  bool isTriangle(int a, int b, int c)  {  return a-b<c && b-c<a && c-a<b;  }};
namespace Triangle{  bool isTriangle(int a, int b, int c)  {    long la(a),lb(b),lc(c);    return a>0 and b>0 and c>0 and           la-lb<lc and la-lc<lb and lb-lc<la and           la+lb>lc and la+lc>lb and lb+lc>la;        }};作者:jdzhangxin链接:http://www.jianshu.com/p/ccbbaa81a9db來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。