-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3_1.java
More file actions
80 lines (71 loc) · 3.41 KB
/
Lab3_1.java
File metadata and controls
80 lines (71 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Scanner;
import java.util.ArrayList;
import java.util.ArrayList;
public class Lab3_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
while (true) {
/* ИНИЦИАЛИЗАЦИЯ И ВВОД МАССИВА */
System.out.println("Для выхода из программы введите любой не числовой символ/последовательность символов");
System.out.println("Введите размерность массива row_count на column_count: ");
System.out.print("row_count= ");
int row_count = scanner.nextInt();
System.out.print("column_count= ");
int column_count = scanner.nextInt();
/*СОЗДАНИЕ МАССИВА*/
int[][] array = create_array(row_count, column_count);
/*ВЫВОД МАССИВА*/
output_array(array, row_count, column_count);
/* ПОДСЧЁТ КОЛИЧЕСТВА ЭЛЕМЕНТОВ */
System.out.print("Сумма индексов должна быть больше чем? ");
int item_to_search = scanner.nextInt();
int count = 0;
ArrayList<Integer> total_array = new ArrayList<Integer>();
for (int i = 0; i < row_count; i++) {
for (int j = 0; j < column_count; j++) {
int tmp[] = new int[2];
if (array[i][j] > 0 && i+j > item_to_search) {
count += 1;
total_array.add(i);
total_array.add(j);
}
}
}
/* ВЫВОД */
output_vector(item_to_search, count, total_array);
}
} catch (Throwable t){
System.out.println("Вы вышли из программы");
}
}
private static int [][] create_array(int row_count, int column_count){
int array[][] = new int[row_count][column_count];
for (int i = 0; i < row_count; i++) {
for (int j = 0; j < column_count; j++) {
array[i][j] = ((int) (Math.random() * 101) - 50);
}
}
return array;
}
private static void output_vector(int item_to_search, int count, ArrayList<Integer> total_array){
System.out.println("Количество элементов с суммой индексов более" + item_to_search + " В массиве равно " + count);
System.out.println("Индексы элементов удовлетворяющих условиям ");
for (int i = 0; i < total_array.size(); i++){
if (i == 0 || i % 2 == 0){
System.out.println("i = " + total_array.get(i));
} else {
System.out.println("j = " + total_array.get(i));
System.out.println("__________________________");
}
}
}
private static void output_array(int[][] array, int row_count, int column_count){
for (int i = 0; i < row_count; i++) {
for (int j = 0; j < column_count; j++) {
System.out.print(" " + array[i][j] + " ");
}
System.out.println("");
}
}
}