Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demo;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.math.RoundingMode;
- import java.nio.charset.StandardCharsets;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Pr01ArrayStatistics {
- public static void main(final String[] args) {
- try (final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
- List<BigInteger> numbers = Arrays.stream(reader.readLine().trim().split("\\s+"))
- .map(BigInteger::new)
- .collect(Collectors.toCollection(ArrayList::new));
- BigInteger min = numbers.stream().min(BigInteger::compareTo).orElse(null);
- BigInteger max = numbers.stream().max(BigInteger::compareTo).orElse(null);
- BigInteger sum = numbers.stream().reduce(BigInteger.ZERO, BigInteger::add);
- BigDecimal avr = new BigDecimal(sum).divide(new BigDecimal(numbers.size()), 15, RoundingMode.HALF_UP);
- System.out.printf("" +
- "Min = %d%n" +
- "Max = %d%n" +
- "Sum = %d%n" +
- "Average = %s%n", min, max, sum, doubleToStringCSharpLike(avr.doubleValue()));
- } catch (IOException | NullPointerException e) {
- e.printStackTrace();
- }
- }
- private static String doubleToStringCSharpLike(double value) {
- int digits = 15;
- DecimalFormat df = new DecimalFormat("0.#");
- df.setMaximumFractionDigits(digits);
- if (Math.abs(value) >= 1d) {
- digits -= df.format(value).split("[.,]")[0].length();
- }
- df.setMaximumFractionDigits(digits);
- return df.format(value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement