/*Java Triangle

Johnny has a younger sister Anne, who is very clever and smart. As she came home from the kindergarten, she told his brother about the task that her kindergartener asked her to solve. The task was just to construct a triangle out of four sticks of different colours. Naturally, one of the sticks is extra. It is not allowed to break the sticks or use their partial length. Anne has perfectly solved this task, now she is asking Johnny to do the same.

The boy answered that he would cope with it without any difficulty. However, after a while he found out that different tricky things can occur. It can happen that it is impossible to construct a triangle of a positive area, but it is possible to construct a degenerate triangle. It can be so, that it is impossible to construct a degenerate triangle even. As Johnny is very lazy, he does not want to consider such a big amount of cases, he asks you to help him.

Input

The first line of the input contains integer number  T, the number of test cases in this test. Then following T lines of input containing four space-separated positive integer numbers not exceeding 100 — lengthes of the sticks.

Output

For each test case, output TRIANGLE if it is possible to construct a non-degenerate triangle. Output SEGMENT if the first case cannot take place and it is possible to construct a degenerate triangle. Output IMPOSSIBLE if it is impossible to construct any triangle. Remember that you are to use three sticks. It is not allowed to break the sticks or use their partial length.

正常三角形(任两边之和大于第三边)

退化三角形(两边之和等于第三边)

不可能三角形(两边之和小于第三边)

        我们知道,判断三角形成不成立不能看长边之和是否大于短边,而应反过来,所以先从小到

大排序,将边长存在数组里。

        下面的东西解释不太清,只能先描述一下写出的算法,回头再想想再回来解释,嘤!

一开始我是打算只判断前三个,看这三个里面较小的两个之和与较大的大小关系; 后来发现即使

有时候前三个不满足正常三角形,后三个也能满足。其他组合就不像这两组这么有代表性和说服力

了。(就是这里,说不上来为什么是这样) 所以就设置两个标志flag1和flag2。

        取排序后数组的前三个元素sticks[0/1/2],记 flag1为sticks[0]+sticks[1]-sticks[2];flag2为

sticks[1]+sticks[2]-sticks[3]。

        flag1和flag2作为判断条件时有以下几种逻辑关系:(个人觉得这三句话很有灵性,尤其是else

的使用)

if (flag1<0&&flag2<0)
        System.out.println("IMPOSSIBLE");

            else if (flag1>0||flag2>0)
                System.out.println("TRIANGLE");

            else if (flag1==0||flag2==0)
                System.out.println("SEGMENT");

完整代码:

package homework3;
import java.util.*;
public class Triangle {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int num = s.nextInt();
		for(int i=0;i<num;i++) {
			int[] sticks=new int[4];
			sticks[0]=s.nextInt();sticks[1]=s.nextInt();
			sticks[2]=s.nextInt();sticks[3]=s.nextInt();
			Arrays.sort(sticks);
			int flag1=sticks[0]+sticks[1]-sticks[2];
			int flag2=sticks[1]+sticks[2]-sticks[3];
			if(flag1<0&&flag2<0)
				System.out.println("IMPOSSIBLE");
			else if(flag1>0||flag2>0)
				System.out.println("TRIANGLE");
			else if(flag1==0||flag2==0)
				System.out.println("SEGMENT");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李 董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值