Advertisement
Guest User

Untitled

a guest
May 28th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4.  *  
  5.  *  Answer to the following question:
  6.  *
  7.  *  selam,
  8.  *  diyelim guzellik yarismasinda 20 kisi var. 10 kisi bir sonraki tura cikacak. ben de
  9.  *  5 kisilik tahmin yapiyorum. jurinin sectigi 10 arasinda benim listededen 4 ve üstü
  10.  *  (4 ve 5 yani) kisi olmasinin olasiligi nedir?
  11.  *
  12.  */
  13. public class OlasilikSorusu {
  14.    
  15.     public static void main(String[] args){
  16.        
  17.         int[] nominees = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  18.         int[] jurysChoices = new int[10];
  19.         int[] myGuess = new int[5];
  20.         int myScore = 0; // number of nominees i guessed right
  21.         int myOverallChance = 0; // the number of runs i scores at least 4
  22.  
  23.         // jury decides only once
  24.         jurysChoices = pickNRandom(nominees, 10);
  25.        
  26. //      // see how my random guess looks like
  27. //      myGuess = pickNRandom(nominees, 5);    
  28. //      printArray(jurysChoices);
  29. //      printArray(myGuess);
  30. //      System.out.println(numberOfIntersection(jurysChoices, myGuess));
  31.        
  32.         // make 10000 guesses, see success rate
  33.         int numberOfRuns = 10000;
  34.         for(int i=0; i<numberOfRuns; i++){
  35.             myGuess = pickNRandom(nominees, 5);
  36.             myScore = numberOfIntersection(jurysChoices, myGuess);
  37.             if(myScore >= 4 ){
  38.                 myOverallChance ++;
  39.             }
  40.         }
  41.        
  42.         System.out.println("I scored at least 4" + " in " + myOverallChance + "/" + numberOfRuns + " runs.");
  43.        
  44.     }
  45.    
  46.     public static int[] pickNRandom(int[] array, int n) {
  47.  
  48.         List<Integer> list = new ArrayList<Integer>(array.length);
  49.         for (int i : array)
  50.             list.add(i);
  51.         Collections.shuffle(list);
  52.  
  53.         int[] answer = new int[n];
  54.         for (int i = 0; i < n; i++)
  55.             answer[i] = list.get(i);
  56.         Arrays.sort(answer);
  57.  
  58.         return answer;
  59.     }
  60.    
  61.     // some very crude search
  62.     public static int numberOfIntersection(int[] biggerArray, int[] smallerArray){
  63.        
  64.         int intersectionCount = 0;
  65.  
  66.         for(int i=0; i<smallerArray.length; i++){
  67.            
  68.             for(int j:biggerArray){
  69.                 if(smallerArray[i] == j){
  70.                     intersectionCount++;
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.        
  76.         return intersectionCount;
  77.     }
  78.    
  79.    
  80.     public static void printArray(int[] array){
  81.         String s = "";
  82.         for(int i: array){
  83.             s = s + i + " ";
  84.         }
  85.         System.out.println(s);     
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement