This repository was archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaba3.java
More file actions
172 lines (157 loc) · 4.93 KB
/
Copy pathLaba3.java
File metadata and controls
172 lines (157 loc) · 4.93 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.company;
import java.util.Scanner;
import java.util.Random;
public class Main {
static void InputArr( int []arr,Scanner num)
{
for(int i=0;i<arr.length;i++)
{
arr[i]=num.nextInt();
}
}
static void OutputArr( int[] arr)
{
for (int i=0;i< arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
static int minusCount(int[] arr)
{
int count=0;
for (int i =0;i<arr.length; i++)
{
if(arr[i]<0)
count++;
}
return count;
}
static void Task1(Scanner num)
{
byte size;
do {
System.out.println("Введіть розмір массива(>0): ");
size = num.nextByte();
}while(size<0);
int[] arr=new int[size];
boolean key=false;
System.out.println("Введення массива: ");
InputArr(arr, num);
for (int i = 1; i < size; i++) {
if (Math.signum(arr[i])!=Math.signum(arr[i-1])){
key = true;
} else {
key = false;
break;
}
}
if (key) {
System.out.println("Елементи чередуються:");
OutputArr(arr);
} else
{
int minusSize=minusCount(arr);
int[] minus=new int[minusSize];
int j=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<0)
{
minus[j]=arr[i];
j++;
}
}
System.out.println("Елементи не чередуються: ");
System.out.println("Вивід масиву відємних елементів: ");
OutputArr(minus);
}
}
static int NullCount(int[] arr)
{
int count=0;
for(int i=0;i< arr.length;i++)
{
if(arr[i]==0)
count++;
}
return count;
}
static int Bruh(int[]arr)
{
int dobut=1;
int maxIndex=0;
int max=arr[0];
//Визначаєм індекс максимального елемента за модулем
for (int i=0;i<arr.length;i++)
{
if(Math.abs(arr[i])>Math.abs(max)) {
max=arr[i];
maxIndex = i;
}
}
for (int i=maxIndex+1;i< arr.length;i++)
{
dobut=dobut*arr[i];
}
return dobut;
}
static void Task2(Scanner num)
{
byte number;
byte size;
do {
System.out.println("Введіть розмір массива(>0): ");
size = num.nextByte();
}while(size<0);
int[] arr = new int[size];
System.out.println("Ввести массив :\n1. З клавіатури:\n2. Випадковими числами[-100;100]");
number=num.nextByte();
switch (number) {
case 2:
Random rnd = new Random(System.currentTimeMillis());
int min = -100;
int max = 100;
for (int i = 0; i < size; i++) {
arr[i] = min + rnd.nextInt(max - min + 1);
}
break;
case 1:
System.out.println("Введення з клавіатури: ");
InputArr(arr,num);
break;
default:
System.out.println("Невірне значення!");
break;
}
OutputArr(arr);
System.out.println("Кількість нульвих елементів: "+NullCount(arr));
System.out.println("Добуток елементів масиву, розташованих після максимального елемента за модулем: "+Bruh(arr));
}
public static void main(String[] args)
{
Scanner num=new Scanner(System.in);
boolean run=true;
byte number;
while(run) {
System.out.println("Вибертіть номер завдання (1-2, 5-ВИХІД!): ");
number = num.nextByte();
switch (number) {
case 1:
System.out.println("Завдання 1: ");
Task1(num);
break;
case 2:
System.out.println("Завдання 2: ");
Task2(num);
break;
case 5:
System.out.println("Завершення виконання....");
run=false;
break;
default:
System.out.println("Не вірне значення:");
break;
}
}
}
}