Filip_Markoski

[NP] Петка или глава

Nov 12th, 2017
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class LoadedCoinTest {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int probability = scanner.nextInt();
  9.         Coin c = new Coin();
  10.         int heads = 0;
  11.         int n = 1000;
  12.         for (int i = 0; i < n; i++) {
  13.             SIDE side = c.flip();
  14.             if (side == SIDE.HEAD) {
  15.                 heads++;
  16.             }
  17.         }
  18.         if (heads > 450 && heads < 550) {
  19.             System.out.println("YES");
  20.         } else {
  21.             System.out.println("NO");
  22.         }
  23.         c = new LoadedCoin(probability);
  24.         heads = 0;
  25.         for (int i = 0; i < n; i++) {
  26.             SIDE side = c.flip();
  27.             if (side == SIDE.HEAD) {
  28.                 heads++;
  29.             }
  30.         }
  31.         if (heads > probability * 10 - 50 && heads < probability * 10 + 50) {
  32.             System.out.println("YES");
  33.         } else {
  34.             System.out.println("NO");
  35.         }
  36.     }
  37. }
  38.  
  39. enum SIDE {
  40.     HEAD, TAIL
  41. }
  42.  
  43. class Coin {
  44.  
  45.     SIDE side;
  46.  
  47.     public SIDE flip() {
  48.         Random random = new Random();
  49.         boolean isHead = random.nextBoolean();
  50.         if (isHead) {
  51.             return SIDE.HEAD;
  52.         } else {
  53.             return SIDE.TAIL;
  54.         }
  55.     }
  56. }
  57.  
  58. class LoadedCoin extends Coin {
  59.     private int probability;
  60.  
  61.     public LoadedCoin(int probability) {
  62.         this.probability = probability;
  63.     }
  64.  
  65.     @Override
  66.     public SIDE flip() {
  67.         Random random = new Random();
  68.  
  69.         /* You specify the bound inside random.nextInt() */
  70.         /* the bound is exclusive meaning if you want to get 100 you must add +1 to it */
  71.         int n = random.nextInt(101);
  72.  
  73.         if (n < probability) {
  74.             return SIDE.HEAD;
  75.         } else {
  76.             return SIDE.TAIL;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment