Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package com.company;
  2. import java.io.IOException;
  3. import java.util.*;
  4.  
  5.  
  6. public class Main {
  7.     private static final Scanner scanner = new Scanner(System.in);
  8.     /*
  9.      * Дано: n - количество чисел в массиве
  10.      * ar - массив чисел
  11.      * 0 < n < 100
  12.      * -100 <= ar[i] <= 100
  13.      * ‚вычислить нормализованное количество положительных, отрицательных и нулевых элементов
  14.      * Пример:
  15.      * 6
  16.      * -4 3 -9 0 4 1
  17.      *
  18.      * Output
  19.      * 0.500000
  20.      * 0.333333
  21.      * 0.166667
  22.      */
  23.  
  24.     static void func1(int[] arr) {
  25.         double normalizedPositivesCount = Arrays.stream(arr).filter(x -> x > 0).toArray().length / (arr.length * 1.0);
  26.         double normalizedNegativesCount = Arrays.stream(arr).filter(x -> x < 0).toArray().length / (arr.length * 1.0);
  27.         double normalizedZeroesCount = Arrays.stream(arr).filter(x -> x == 0).toArray().length / (arr.length * 1.0);
  28.         System.out.printf("%f%n%f%n%f%n", normalizedPositivesCount, normalizedNegativesCount, normalizedZeroesCount);
  29.     }
  30.  
  31.     public static class Func {
  32.         private int[] arr;
  33.         public Func(int[] arr) {
  34.             this.arr = arr;
  35.         }
  36.  
  37.         public int[] getArr() {
  38.             return arr;
  39.         }
  40.  
  41.         public void setArr(int[] arr) {
  42.             this.arr = arr;
  43.         }
  44.  
  45.         public void calcAndShow() {
  46.             double normalizedPositivesCount = Arrays.stream(arr).filter(x -> x > 0).toArray().length / (arr.length * 1.0);
  47.             double normalizedNegativesCount = Arrays.stream(arr).filter(x -> x < 0).toArray().length / (arr.length * 1.0);
  48.             double normalizedZeroesCount = Arrays.stream(arr).filter(x -> x == 0).toArray().length / (arr.length * 1.0);
  49.             System.out.printf("%f%n%f%n%f%n", normalizedPositivesCount, normalizedNegativesCount, normalizedZeroesCount);
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) throws IOException {
  54. //        System.out.println("Enter n amount:");
  55. //        int n = scanner.nextInt();
  56. //        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  57. //
  58. //        int[] arr = new int[n];
  59. //        System.out.println("Enter element mass:");
  60. //        String[] arrItems = scanner.nextLine().split(" ");
  61. //        System.out.println("Press Enter");
  62. //        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  63. //        for (int i = 0; i < n; i++) {
  64. //            int arrItem = Integer.parseInt(arrItems[i]);
  65. //            arr[i] = arrItem;
  66. //        }
  67. //        func1(arr);
  68. //        scanner.close();
  69.         int[] arr = {-4, 3, -9, 0, 4, 1};
  70.         //func1(arr);
  71.         Func func2 = new Func(arr);
  72.         //func2.calcAndShow();
  73.  
  74.         Runnable func3 = () -> {
  75.             Thread.currentThread().setName("Считаем нормализованные данные");
  76.             System.out.println("Название нити " + Thread.currentThread().getName());
  77.             func2.calcAndShow();
  78.         };
  79.         new Thread(func3).start();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement