8000 Added solution of Yandex Contest/Training on algorithms 1.0/Lesson 1/… · Tamada4a/Algorithms@f6b8302 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit f6b8302

Browse files
committed
Added solution of Yandex Contest/Training on algorithms 1.0/Lesson 1/B. Triangle
1 parent 6db7837 commit f6b8302

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.BufferedReader;
2+
import java.io.FileReader;
3+
import java.io.IOException;
4+
import java.util.TreeMap;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
// This variable will store the lengths of the sides of the triangle, ordered in ascending order of length
9+
TreeMap<Integer, Integer> triangleSides = new TreeMap<>();
10+
11+
// Reading data
12+
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
13+
String line;
14+
while ((line = reader.readLine()) != null) {
15+
try {
16+
int length = Integer.parseInt(line);
17+
if (!triangleSides.containsKey(length)) {
18+
triangleSides.put(length, 0);
19+
}
20+
triangleSides.put(length, triangleSides.get(length) + 1);
21+
} catch (NumberFormatException ignored){}
22+
}
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
27+
// Index of the current element
28+
int i = 0;
29+
// The sum of the sides length
30+
int sum = 0;
31+
// Maximum length value
32+
int max = -1;
33+
34+
// We calculate the sum of the lengths of the smaller sides of the triangle
35+
for (int key : triangleSides.keySet()) {
36+
// If you have reached the longest length
37+
if (i == (triangleSides.size() - 1)) {
38+
// Here we subtract one, since there can be two sides with the longest side length
39+
sum += key * (triangleSides.get(key) - 1);
40+
max = key;
41+
}
42+
// Otherwise, we add the length of the side multiplied by the number of such sides
43+
else {
44+
sum += key * triangleSides.get(key);
45+
}
46+
i++;
47+
}
48+
49+
// You can build a triangle if the sum of the lengths of the smaller sides is greater than the length of the larger side
50+
if (sum > max) {
51+
System.out.print("YES");
52+
} else {
53+
System.out.print("NO");
54+
}
55+
}
56+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# B. Triangle
2+
3+
<table>
4+
<tr>
5+
<td>Time limit</td>
6+
<td>1 second</td>
7+
</< 8000 span class="pl-ent">tr>
8+
<tr>
9+
<td>Memory limit</td>
10+
<td>64Mb</td>
11+
</tr>
12+
<tr>
13+
<td>Input</td>
14+
<td>standart input or input.txt</td>
15+
</tr>
16+
<tr>
17+
<td>Output</td>
18+
<td>standart output or output.txt</td>
19+
</tr>
20+
</table>
21+
22+
Three natural numbers are given. Is it possible to build a triangle with such sides? If possible, print the string YES, otherwise print the string NO.
23+
24+
A triangle is three points that do not lie on the same straight line.
25+
26+
## Input format
27+
Three natural numbers are entered.
28+
29+
## Output format
30+
Print the answer to the problem.
31+
32+
## Example 1
33+
<table>
34+
<thead>
35+
<tr>
36+
<th align= "left">Input</th>
37+
<th align= "left">Output</th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
<tr>
42+
<td>
43+
3</br>
44+
4</br>
45+
5</br>
46+
</br>
47+
</br>
48+
</td>
49+
<td>
50+
YES
51+
</td>
52+
</tr>
53+
</tbody>
54+
</table>
55+
56+
## Example 2
57+
<table>
58+
<thead>
59+
<tr>
60+
<th align= "left">Input</th>
61+
<th align= "left">Output</th>
62+
</tr>
63+
</thead>
64+
<tbody>
65+
<tr>
66+
<td>
67+
3</br>
68+
5</br>
69+
4</br>
70+
</br>
71+
</br>
72+
</td>
73+
<td>
74+
YES
75+
</td>
76+
</tr>
77+
</tbody>
78 F438 +
</table>
79+
80+
## Example 3
81+
<table>
82+
<thead>
83+
<tr>
84+
<th align= "left">Input</th>
85+
<th align= "left">Output</th>
86+
</tr>
87+
</thead>
88+
<tbody>
89+
<tr>
90+
<td>
91+
4</br>
92+
5</br>
93+
3
94+
</td>
95+
<td>
96+
YES
97+
</td>
98+
</tr>
99+
</tbody>
100+
</table>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# B. Треугольник
2+
3+
<table>
4+
<tr>
5+
<td>Ограничение времени</td>
6+
<td>1 секунда</td>
7+
</tr>
8+
<tr>
9+
<td>Ограничение памяти</td>
10+
<td>64Mb</td>
11+
</tr>
12+
<tr>
13+
<td>Ввод</td>
14+
<td>стандартный ввод или input.txt</td>
15+
</tr>
16+
<tr>
17+
<td>Вывод</td>
18+
<td>стандартный вывод или output.txt</td>
19+
</tr>
20+
</table>
21+
22+
Даны три натуральных числа. Возможно ли построить треугольник с такими сторонами. Если это возможно, выведите строку YES, иначе выведите строку NO.
23+
24+
Треугольник — это три точки, не лежащие на одной прямой.
25+
26+
## Формат ввода
27+
Вводятся три натуральных числа.
28+
29+
## Формат вывода
30+
Выведите ответ на задачу.
31+
32+
## Пример 1
33+
<table>
34+
<thead>
35+
<tr>
36+
<th align= "left">Ввод</th>
37+
<th align= "left">Вывод</th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
<tr>
42+
<td>
43+
3</br>
44+
4</br>
45+
5</br>
46+
</br>
47+
</br>
48+
</td>
49+
<td>
50+
YES
51+
</td>
52+
</tr>
53+
</tbody>
54+
</table>
55+
56+
## Пример 2
57+
<table>
58+
<thead>
59+
<tr>
60+
<th align= "left">Ввод</th>
61+
<th align= "left">Вывод</th>
62+
</tr>
63+
</thead>
64+
<tbody>
65+
<tr>
66+
<td>
67+
3</br>
68+
5</br>
69+
4</br>
70+
</br>
71+
</br>
72+
</td>
73+
<td>
74+
YES
75+
</td>
76+
</tr>
77+
</tbody>
78+
</table>
79+
80+
## Пример 3
81+
<table>
82+
<thead>
83+
<tr>
84+
<th align= "left">Ввод</th>
85+
<th align= "left">Вывод</th>
86+
</tr>
87+
</thead>
88+
<tbody>
89+
<tr>
90+
<td>
91+
4</br>
92+
5</br>
93+
3
94+
</td>
95+
<td>
96+
YES
97+
</td>
98+
</tr>
99+
</tbody>
100+
</table>

0 commit comments

Comments
 (0)
0