Advertisement
ben1939

targil

Nov 6th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. int naive_strategy(int n)
  7. {
  8. int k, rest;
  9.  
  10. k=rand()%(n/2)+1;
  11. rest= n-k ;
  12.  
  13. return rest;
  14. }
  15.  
  16.  
  17. int smallest_higher_power2(int n)
  18. {
  19.   int i=1;
  20. while (pow (2.0,i) < n)
  21.    i=i+1;
  22. return i;
  23. }
  24.  
  25.  
  26. int smart_strategy(int n)
  27. {
  28. int a, rest;
  29. a=smallest_higher_power2(n);  
  30.     if (n== (pow(2.0,a)-1))
  31. rest= naive_strategy(n);
  32.     else rest= pow (2.0,a-1) -1;
  33. return rest;
  34. }
  35.  
  36.  
  37. int play(int s1, int s2, int k, int start)
  38. {
  39.  
  40. while  (k>0) {  
  41.  
  42.       if (start== 1) {
  43. if (s1 == 1)  k=naive_strategy(k);
  44. else k= smart_strategy (k); start=2;
  45. }
  46.      else {
  47. if (s2 == 1) k=naive_strategy(k);  
  48. else k= smart_strategy (k); start=1;
  49. }
  50.  
  51. }
  52.  
  53. return start;
  54. }
  55.  
  56. int compare(int s1, int s2, int k, int numOfPlays
  57. {
  58. int start, i, win, x=0;
  59.    
  60. for(i=1; i<=numOfPlays; i++) {
  61.    start=rand()%2+1;
  62.    win= play(s1, s2, k, start);
  63.          if (win == 1) x++;
  64.            }
  65.  
  66. return x;
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.     int s1, s2, k, n, temp;
  73.     printf("enter parameters \n");
  74.     scanf("%d%d%d%d",&s1, &s2, &k ,&n);
  75.     temp = compare(s1, s2, k, n);
  76.     printf("the number of win is%d \n", temp);
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement