fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.*;
  7. import java.math.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. for (Map.Entry entry : getChances(10, 20, 15).entrySet()) System.out.println(entry.getKey() + ": " + entry.getValue());
  15. }
  16.  
  17. @SafeVarargs
  18. private static final int[] reverse(final int... array) {
  19. int[] ret = new int[array.length];
  20. for (int i = array.length-1; i > 0; i--){
  21. ret[Math.abs(0-i)] = array[i];
  22. }
  23. return ret;
  24. }
  25.  
  26. private static Map<Integer, Float> getChances(int min, int max, int close) {
  27. int differ = max - min;
  28. if (close > max || close < min) {
  29. close = differ + min;
  30. }
  31. int[] numbers = IntStream.rangeClosed(min, max).toArray();
  32. if (numbers[numbers.length-1] == close) {
  33. numbers = reverse(numbers);
  34. }
  35. Map<Integer, Float> chances = new LinkedHashMap<>();
  36. chances.put(close, 50f);
  37. for (int i = min; i <= max; i++) {
  38. float chance = new BigDecimal(50f).divide(new BigDecimal(Math.abs(i-close)*2), BigDecimal.ROUND_HALF_EVEN).setScale(2, i < close ? BigDecimal.ROUND_CEILING : BigDecimal.ROUND_FLOOR).floatValue();
  39. chances.putIfAbsent(i, chance);
  40. }
  41. return chances;
  42. }
  43. }
Runtime error #stdin #stdout #stderr 0.19s 2841600KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at java.math.BigDecimal.divideAndRound(BigDecimal.java:4106)
	at java.math.BigDecimal.divide(BigDecimal.java:5198)
	at java.math.BigDecimal.divide(BigDecimal.java:1561)
	at java.math.BigDecimal.divide(BigDecimal.java:1622)
	at Ideone.getChances(Main.java:38)
	at Ideone.main(Main.java:14)