CodeForces 6A-Triangle(枚举/暴力)

简介: 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;
}


相关文章
|
8月前
|
Java
【每日一题Day121】LC1139最大的以 1 为边界的正方形 | 前缀和数组 + 枚举
【每日一题Day121】LC1139最大的以 1 为边界的正方形 | 前缀和数组 + 枚举
47 0
洛谷P2141珠心算测验 (枚举&&暴力解法)
洛谷P2141珠心算测验 (枚举&&暴力解法)
132 0
|
7月前
|
存储 人工智能 决策智能
每日一题——博弈论(枚举与暴力)
每日一题——博弈论(枚举与暴力)
46 1
每日一题——博弈论(枚举与暴力)
|
7月前
|
C语言
每日刷题——杭电2156.分数矩阵和杭电2024.C语言合法标识符
每日刷题——杭电2156.分数矩阵和杭电2024.C语言合法标识符
55 1
codeforces144——D. Missile Silos(最短路+枚举)
codeforces144——D. Missile Silos(最短路+枚举)
100 0
codeforces144——D. Missile Silos(最短路+枚举)
|
机器学习/深度学习 Java
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)
107 0
|
人工智能
Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
题意: 给出一个长度为n的数组,有m个询问,每次询问给出一个区间,问这个区间内有多少个数x恰好出现x次
134 0
Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
☆打卡算法☆LeetCode 6、Z字形变换 算法解析
“将给定的字符串以给定的行数记性Z字形排列。”
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard out...
1141 0