首页 > 试题广场 >

三角形

[编程题]三角形
  • 热度指数:7119 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定三条边,请你判断一下能不能组成一个三角形。

输入描述:
输入包含多组数据,每组数据包含三个正整数a、b、c(1≤a, b, c≤10^100)。


输出描述:
对应每一组数据,如果它们能组成一个三角形,则输出“Yes”;否则,输出“No”。
示例1

输入

1 2 3
2 2 2

输出

No
Yes
while (in.hasNext()){
                String line = in.nextLine();
                String[] part = line.split(" ");
                BigInteger a = new BigInteger(part[0]);
                BigInteger b = new BigInteger(part[1]);
                BigInteger c = new BigInteger(part[2]); //            BigInteger max = a > b ?(a > c ? a : c) : (b > c ? b : c);  //找最大数  BigInteger max = (a.compareTo(b) > 0)
                        ? (a.compareTo(c) > 0 ? a : c)
                        : (b.compareTo(c) > 0 ? b : c); //最小2边和  BigInteger sum = max.equals(a) ? b.add(c) : (max.equals(b) ? a.add(c) : a.add(b)); //判断:sum>max  String result = (sum.compareTo(max)>0)? "Yes" : "No";
                System.out.println(result);
            }
        }    }
}
发表于 2026-01-15 19:23:47 回复(0)
import java.math.BigInteger;
import java.util.Scanner;

public class Demo64 {
    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in)){
            BigInteger a = null;
            BigInteger b = null;
            BigInteger c = null;
            while (input.hasNextBigInteger()){
                a = input.nextBigInteger();
                b = input.nextBigInteger();
                c = input.nextBigInteger();

                BigInteger max = a.max(b.max(c));
                BigInteger min = a.min(b.min(c));
                BigInteger mid = a.add(b.add(c)).subtract(max).subtract(min);

                String result = (max.compareTo(min.add(mid)) < 0) ? "Yes" : "No";
                System.out.println(result);
            }

        }

    }
}

发表于 2026-01-15 19:06:30 回复(0)
import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            BigDecimal a = sc.nextBigDecimal();
            BigDecimal b = sc.nextBigDecimal();
            BigDecimal c = sc.nextBigDecimal();
            if(a.add(b).compareTo(c) > 0 
              && a.add(c).compareTo(b) > 0
              &&c.add(b).compareTo(a) > 0){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}

发表于 2022-05-07 09:58:08 回复(0)
//最小值+中间值>最大值即成立   可自行证明
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String str = sc.nextLine();
            String[] ss = str.split(" ");

            BigInteger a = new BigInteger(ss[0]);
            BigInteger b = new BigInteger(ss[1]);
            BigInteger c = new BigInteger(ss[2]);

            //找出a b c 三边最大边,最小,和中间值
            //如果max<middle+min,就可以构成三角形

            BigInteger max=null;
            BigInteger mid=null;
            BigInteger min=null;


            List<BigInteger> list = new ArrayList();
            list.add(a);
            list.add(b);
            list.add(c);
            list.sort(null);

            //成立条件
            if(list.get(0).add(list.get(1)).compareTo(list.get(2))>0)
            {
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
    }
}

发表于 2019-10-10 21:01:30 回复(0)