CodeForces 6A-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.

输入: 

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. 

样例输入: 

4 2 1 3

7 2 2 4

3 5 9 1

(不是多实例,只是三组测试样例,需分别输入) 

样例输出: 

TRIANGLE

SEGMENT

IMPOSSIBLE  

解题思路: 

翻译:给定 4 根木棍的长度,如果它们中存在 3 根木棍可以组成三角形,输出 TRIANGLE ;如果它们无法组成三角形,但是它们中存在 3 根木棍可以组成退化的三角形(任意两边之和大于等于第三边,但是不是三角形),输出 SEGMENT ;否则,输出 IMPOSSIBLE 。 

输入:一行 4 个整数,4 根木棍的长度。

输出:如果它们中存在 3 根木棍可以组成三角形,输出 TRIANGLE ;如果它们无法组成三角形,但是它们中存在3根木棍可以组成退化的三角形,输出 SEGMENT ;否则,输出 IMPOSSIBLE

直接暴力求解!!!看下面的代码:👇👇👇

AC Code: 

#include<bits/stdc++.h>
using namespace std;
int main() {
	int a[5];
	for(int i=1;i<=4;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=4;i++) {
		for(int j=1;j<=4&&j!=i;j++) {
			for(int k=1;k<=4&&k!=i&&k!=j;k++) {
				if(a[i]+a[j]>a[k]&&a[i]+a[k]>a[j]&&a[j]+a[k]>a[i]) {
					printf("TRIANGLE\n");
					return 0;
				}
			}
		}
	}
	for(int i=1;i<=4;i++) {
		for(int j=1;j<=4&&j!=i;j++) {
			for(int k=1;k<=4&&k!=i&&k!=j;k++) {
				if(a[i]+a[j]>=a[k]&&a[i]+a[k]>=a[j]&&a[j]+a[k]>=a[i]) {
					printf("SEGMENT\n");
					return 0;
				}
			}
		}
	}
	printf("IMPOSSIBLE\n");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章