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 four space-separated positive integer numbers not exceeding 100 — lengthes of the sticks.
OutputOutput 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.
Examples4 2 1 3
TRIANGLE
7 2 2 4
SEGMENT
3 5 9 1
IMPOSSIBLE
其中三个长度可以构成一个正常的三角形,两边之和大于第三边那么就输出
TRIANGLE如果会有三个线段两边之和等于第三边,说明它退化成了一条直线,输出
SEGMENT
否则输出IMPOSSIBLE
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int a[5];
for(int i = 0; i < 4; i++){
cin >> a[i];
}
sort(a,a+4);
if(a[0]+a[1]>a[2]||a[0]+a[2]>a[3]||a[1]+a[2]>a[3]||a[0]+a[1]>a[3]){
cout << "TRIANGLE\n";
return 0;
}
if(a[0]+a[1]==a[2]||a[0]+a[2]==a[3]||a[1]+a[2]==a[3]||a[0]+a[1]==a[3]){
cout << "SEGMENT\n";
return 0;
}
cout << "IMPOSSIBLE\n";
return 0;
}
本文介绍了一个编程挑战,任务是根据四根不同颜色的棍子的长度判断能否构成正常或退化的三角形。通过代码实现,文章展示了如何使用条件判断来解决这一问题。
146

被折叠的 条评论
为什么被折叠?



