zoukankan      html  css  js  c++  java
  • 给三个int,判断是否可构成三角形算法

    哎,今天跟同事讨论算法,有一个女生给我出了这样一个题目,bool Test(int a,int b,int c)感觉很简单,实际呢?自己考虑的不够全面。在得到了提示之后呢,也还是找不到很好的解决方案。我是有多菜已经懒得再提了,只是今天遇到了,不能保证以后不会遇到。对于我这种脑袋很简单的童鞋,只能在积累中逐渐成长了。短期内,遇到什么算法题,自己先思考,然后呢,看看别人的最优解跟自己的差距。不能光有嘴把式,还要有真把式,我要求自己,再简单的算法,也都必须自己实现一下,看一遍记不住,就看一百遍,背也要背下来。其实算法不是很适合我这种菜鸟的,逻辑能力也不行,高数也不行,那什么又是我擅长的呢?也许我现在不行是因为看的算法太少呢?跟写文章一样,肚子里有东西自然可以出口成章啦~我还是很乐观的,我还是很感动于我在知乎上看到的那句话。

    “说好了写一辈子代码,少一天少一分钟少一秒都不算一辈子。”

    好,下面给出代码:

        class TestTriangle
        {
            public static bool isTri(int a, int b, int c)
            {
                if (a > 0 && b > 0 && c > 0)  
                {
                    if ((a > c-b && Math.Abs(a - b) < c) || (c > a-b && Math.Abs(c - b) < a) || (a> b-c && Math.Abs(a - c) < b))
                    {
                        return true;
                    }
                    else return false;
                }
                else return false;
            }  
        }
        class program
        {
            static void Main()
            {
                while (true) {
                    Console.WriteLine("Please input the first edge:");
                    int a = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Please input the Second edge:");
                    int b = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Please input the third edge:");
                    int c = Int32.Parse(Console.ReadLine());
                Console.WriteLine(TestTriangle.isTri(a, b, c));
                }
            }
        }
    

     话说为什么不能判断a+b>c,因为一旦a和b等于了Int32.MaxValue,那他们的和不就overflow了么?这是关键。但是我却没有想到。

    加油!去学习C#了!

  • 相关阅读:
    事件基础 (事件对象 阻止事件冒泡)
    document
    linux 下使用shell命令iostat监控iowait是否超负载
    使用shell来监控linux的io
    linux下使用awk命令按时间段筛选日志
    gulp 压缩js
    cf776D Mahmoud and a Dictionary
    P1313 计算系数
    __builtin_popcount() 函数
    HDU 6386 Age of Moyu
  • 原文地址:https://www.cnblogs.com/jin-wen-xin/p/4060237.html
Copyright © 2011-2022 走看看