Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int naive_strategy(int n)
- {
- int k, rest;
- k=rand()%(n/2)+1;
- rest= n-k ;
- return rest;
- }
- int smallest_higher_power2(int n)
- {
- int i=1;
- while (pow (2.0,i) < n)
- i=i+1;
- return i;
- }
- int smart_strategy(int n)
- {
- int a, rest;
- a=smallest_higher_power2(n);
- if (n== (pow(2.0,a)-1))
- rest= naive_strategy(n);
- else rest= pow (2.0,a-1) -1;
- return rest;
- }
- int play(int s1, int s2, int k, int start)
- {
- while (k>0) {
- if (start== 1) {
- if (s1 == 1) k=naive_strategy(k);
- else k= smart_strategy (k); start=2;
- }
- else {
- if (s2 == 1) k=naive_strategy(k);
- else k= smart_strategy (k); start=1;
- }
- }
- return start;
- }
- int compare(int s1, int s2, int k, int numOfPlays
- {
- int start, i, win, x=0;
- for(i=1; i<=numOfPlays; i++) {
- start=rand()%2+1;
- win= play(s1, s2, k, start);
- if (win == 1) x++;
- }
- return x;
- }
- int main()
- {
- int s1, s2, k, n, temp;
- printf("enter parameters \n");
- scanf("%d%d%d%d",&s1, &s2, &k ,&n);
- temp = compare(s1, s2, k, n);
- printf("the number of win is%d \n", temp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement