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;
}

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