Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 69368ae

Browse files
authored
Merge pull request #600 from EgorBurov246/features/lab29
Features/lab29
2 parents 49046ee + 1e5a4d3 commit 69368ae

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p29</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Scanner;
4+
5+
public abstract class CitiesAndRoads {
6+
public static void main(String[] args) {
7+
// Используем try-with-resources для автоматического закрытия Scanner
8+
try (Scanner scanner = new Scanner(System.in)) {
9+
System.out.print("Введите количество городов (n): ");
10+
int numCities = scanner.nextInt();
11+
12+
// Проверка на допустимое значение numCities
13+
if (numCities < 0 || numCities > 100) {
14+
System.out.println("Количество городов должно быть в диапазоне от 0 до 100.");
15+
return;
16+
}
17+
18+
// Создание объекта CityRoads
19+
CityRoads cityRoads = new CityRoads(numCities);
20+
cityRoads.fillMatrix(scanner);
21+
22+
// Подсчет и вывод количества дорог
23+
int totalRoads = cityRoads.countRoads();
24+
System.out.println("Количество дорог на планете 'Neptune': " + totalRoads);
25+
} // Scanner автоматически закрывается здесь
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Scanner;
4+
5+
class CityRoads {
6+
private int[][] adjacencyMatrix;
7+
private int numberOfCities;
8+
9+
// Конструктор для инициализации
10+
public CityRoads(int numberOfCities) {
11+
this.numberOfCities = numberOfCities;
12+
this.adjacencyMatrix = new int[numberOfCities][numberOfCities];
13+
}
14+
15+
// Метод для заполнения матрицы смежности
16+
public void fillMatrix(Scanner scanner) {
17+
System.out.println("Введите матрицу смежности:");
18+
for (int i = 0; i < numberOfCities; i++) {
19+
for (int j = 0; j < numberOfCities; j++) {
20+
while (true) {
21+
try {
22+
this.adjacencyMatrix[i][j] = scanner.nextInt();
23+
if (this.adjacencyMatrix[i][j] != 0 && this.adjacencyMatrix[i][j] != 1) {
24+
throw new IllegalArgumentException("Значение должно быть 0 или 1");
25+
}
26+
break; // Выход из цикла, если ввод корректен
27+
} catch (Exception e) {
28+
System.out.println("Ошибка ввода. Пожалуйста, введите 0 или 1:");
29+
scanner.next(); // Удаляем неверный ввод
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
// Метод для подсчета дорог
37+
public int countRoads() {
38+
int roadCount = 0;
39+
for (int i = 0; i < numberOfCities; i++) {
40+
for (int j = 0; j < numberOfCities; j++) {
41+
// Проверяем только выше главной диагонали
42+
if (j > i && adjacencyMatrix[i][j] == 1) {
43+
roadCount++;
44+
}
45+
}
46+
}
47+
return roadCount;
48+
}
49+
}

students/23K0815/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@
3737
<module>23K0815-p26</module>
3838
<module>23K0815-p27</module>
3939
<module>23K0815-p28</module>
40+
<module>23K0815-p29</module>
4041
</modules>
4142
</project>

0 commit comments

Comments
 (0)