[CF6A]Triangle 题解

[CF6A]Triangle 题解

题目地址:Codeforces:Problem – 6A – Codeforces、洛谷:CF6A 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.

题意简述

有4根木棍,长度已知,问是否能选出3根组成一个三角形或者退化的三角形(两边之和等于第三边)。

输入输出格式

输入格式:
The first line of the input contains four space-separated positive integer numbers not exceeding 100 — lengthes of the sticks.

输出格式:
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.

输入输出样例

输入样例#1:

4 2 1 3

输出样例#1:

TRIANGLE

输入样例#2:

7 2 2 4

输出样例#2:

SEGMENT

输入样例#3:

3 5 9 1

输出样例#3:

IMPOSSIBLE

题解

参考资料:题解 CF6A 【Triangle】 – Heartlessly 的博客 – 洛谷博客

这个题目作为A题自然是不难的,写这篇题解主要是为了记录下这个A题简化的有趣思想。

首先,我们对4根木棍按长度排序,有序序列具有很好的性质。考虑从中选出3根木棍的所有选择情况:
1 2 3 | 1 3 4 | 2 3 4 | 1 2 4
如果选出的木棍能组成三角形,1+2>4的时候同时满足1+2>3,因此1 2 4包含在1 2 3的情况中;同理1 3 4包含在2 3 4中。组成退化的三角形同理。

经过上面的分析,我们只需要考虑1 2 3和2 3 4两种情况即可。

这种合并同类选项、减少情况数量的方法在其他题目中也有应用,可以有效减少代码细节,是很有意思的做法。

代码

// Code by KSkun, 2019/8
#include <cstdio>
#include <cctype>

#include <algorithm>

typedef long long LL;

inline char fgc() {
	static char buf[100000], * p1 = buf, * p2 = buf;
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
		? EOF : *p1++;
}

inline LL readint() {
	LL res = 0, neg = 1; char c = fgc();
	for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
	for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
	return res * neg;
}

inline char readsingle() {
	char c;
	while(!isgraph(c = fgc())) {}
	return c;
}

const int MAXN = 5;

int a[5];

int main() {
	for(int i = 1; i <= 4; i++) {
		a[i] = readint();
	}
	std::sort(a + 1, a + 5);
	if(a[1] + a[2] > a[3] || a[2] + a[3] > a[4]) puts("TRIANGLE");
	else if(a[1] + a[2] == a[3] || a[2] + a[3] == a[4]) puts("SEGMENT");
	else puts("IMPOSSIBLE");
	return 0;
}


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据