Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.util.*;
- public class Main {
- private static final Scanner scanner = new Scanner(System.in);
- /*
- * Дано: n - количество чисел в массиве
- * ar - массив чисел
- * 0 < n < 100
- * -100 <= ar[i] <= 100
- * ‚вычислить нормализованное количество положительных, отрицательных и нулевых элементов
- * Пример:
- * 6
- * -4 3 -9 0 4 1
- *
- * Output
- * 0.500000
- * 0.333333
- * 0.166667
- */
- static void func1(int[] arr) {
- double normalizedPositivesCount = Arrays.stream(arr).filter(x -> x > 0).toArray().length / (arr.length * 1.0);
- double normalizedNegativesCount = Arrays.stream(arr).filter(x -> x < 0).toArray().length / (arr.length * 1.0);
- double normalizedZeroesCount = Arrays.stream(arr).filter(x -> x == 0).toArray().length / (arr.length * 1.0);
- System.out.printf("%f%n%f%n%f%n", normalizedPositivesCount, normalizedNegativesCount, normalizedZeroesCount);
- }
- public static class Func {
- private int[] arr;
- public Func(int[] arr) {
- this.arr = arr;
- }
- public int[] getArr() {
- return arr;
- }
- public void setArr(int[] arr) {
- this.arr = arr;
- }
- public void calcAndShow() {
- double normalizedPositivesCount = Arrays.stream(arr).filter(x -> x > 0).toArray().length / (arr.length * 1.0);
- double normalizedNegativesCount = Arrays.stream(arr).filter(x -> x < 0).toArray().length / (arr.length * 1.0);
- double normalizedZeroesCount = Arrays.stream(arr).filter(x -> x == 0).toArray().length / (arr.length * 1.0);
- System.out.printf("%f%n%f%n%f%n", normalizedPositivesCount, normalizedNegativesCount, normalizedZeroesCount);
- }
- }
- public static void main(String[] args) throws IOException {
- // System.out.println("Enter n amount:");
- // int n = scanner.nextInt();
- // scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
- //
- // int[] arr = new int[n];
- // System.out.println("Enter element mass:");
- // String[] arrItems = scanner.nextLine().split(" ");
- // System.out.println("Press Enter");
- // scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
- // for (int i = 0; i < n; i++) {
- // int arrItem = Integer.parseInt(arrItems[i]);
- // arr[i] = arrItem;
- // }
- // func1(arr);
- // scanner.close();
- int[] arr = {-4, 3, -9, 0, 4, 1};
- //func1(arr);
- Func func2 = new Func(arr);
- //func2.calcAndShow();
- Runnable func3 = () -> {
- Thread.currentThread().setName("Считаем нормализованные данные");
- System.out.println("Название нити " + Thread.currentThread().getName());
- func2.calcAndShow();
- };
- new Thread(func3).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement