Algorithm
Problem Name: beecrowd | 1929
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1929
Triangle
By Guilherme Albuquerque Pinto, Universidade Federal de Juiz de Fora Brazil
Timelimit: 1
Ana and her friends are making a geometry work to school, they need to create various triangles on a chart, with a few sticks of different lengths. Soon they realized that you can not form triangles with three rods of any lengths: if one of the rods is too large relative to the other two, you can't form a triangle.
In this issue, you have to help Ana and her friends to determine if, given the lengths of four rods, it is or not possible to select three rods, among the four, and form a triangle.
Input
The entry consists of one line containing four integers A, B, C and D (1 ≤ A, B, C, D ≤ 100).
Output
Your program should only produce a line containing only one character, which must be 'S' if it is possible to form the triangle or 'N' if you can not form a triangle.
Input Example | Output Example |
6 9 22 15 |
S |
14 40 12 60 |
N |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[4];
for(int i = 0; i < 4; i++)
cin>>a[i];
sort(a,a+4);
if(a[0]+a[1]>a[2])
cout << "S\n";
else if(a[0]+a[2]>a[3])
cout << "S\n";
else if(a[1]+a[2]>a[3])
cout << "S\n";
else
cout << "N\n";
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
const line = lines.shift().split(' ').map(Number);
const vetor = line.sort((a, b) => a - b);
if (
(Math.abs(vetor[0] - vetor[1]) < vetor[2] &&
vetor[2] < vetor[0] + vetor[1]) ||
(Math.abs(vetor[0] - vetor[1]) < vetor[3] &&
vetor[3] < vetor[0] + vetor[1]) ||
(Math.abs(vetor[0] - vetor[2]) < vetor[3] &&
vetor[3] < vetor[0] + vetor[2]) ||
(Math.abs(vetor[1] - vetor[2]) < vetor[3] && vetor[3] < vetor[1] + vetor[2])
) {
console.log('S');
} else {
console.log('N');
}
Copy The Code &
Try With Live Editor